LeetCode链表练习

876. Middle of the Linked List

Description

Given a non-empty, singly linked list with head node head, return a middle node of linked list.
If there are two middle nodes, return the second middle node.

Example 1:

Input: [1,2,3,4,5]
Output: Node 3 from this list (Serialization: [3,4,5])
The returned node has value 3.  (The judge's serialization of this node is [3,4,5]).
Note that we returned a ListNode object ans, such that:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL.

Example 2:

Input: [1,2,3,4,5,6]
Output: Node 4 from this list (Serialization: [4,5,6])
Since the list has two middle nodes with values 3 and 4, we return the second one.

思路

使用快慢指针,快指针一次移动2个节点,慢指针移动一个节点。当快指针到达链表最后一个元素或者为空时,慢指针指向中心节点。

完整代码

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode middleNode(ListNode head) {
        if(head == null){
            return null;
        }
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
}

21. Merge Two Sorted Lists

Description

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

思路

  1. 若l1为空,返回l2;反之亦然。
  2. 比较两链表表头节点,将小的节点赋给新的链表,将原链表移动一个节点,以备后续使用。
  3. 依次遍历两链表,取出较小节点复制给新链表;当某一链表为空时,退出循环。
  4. 将还未遍历结束的链表剩余节点加入新链表中。

完整代码

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null){
            return l2;
        }
        if(l2 == null){
            return l1;
        }
        ListNode p = l1;
        ListNode q = l2;
        ListNode merge = null;
        if(p.val <= q.val){
            merge = p;
            p = p.next;
        } else{
            merge = q;
            q = q.next;
        }
        ListNode curr = merge;
        while(p != null && q != null){
            if(p.val <= q.val){
                curr.next = p;
                curr = curr.next;
                p = p.next;
            } else {
                curr.next = q;
                curr = curr.next;
                q = q.next;
            }
        }
        if(p == null){
            curr.next = q;
        } else{
            curr.next = p;
        }
        return merge;
    }
}

206. Reverse Linked List

Description

Reverse a singly linked list.

Example:

Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL

思路

设置三个指针,第一个指针为空,第二个指针为头结点,第三个指针为第二个节点。遍历链表,根据三个指针将链表反转。

完整代码

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null){
            return null;
        }
        ListNode pre = null;
        ListNode p = head;
        ListNode q = p.next;
        while(q != null){
            p.next = pre;
            pre = p;
            p = q;
            q = q.next;
        }
        p.next = pre;
        return p;
    }
}   

你可能感兴趣的:(LeetCode链表练习)