链表

第1天

解析

21.合并两个有序链表(简单)

class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode res = new ListNode(0), cur = res;
        while(l1 != null && l2 != null){
            if(l1.val < l2.val){
                cur = cur.next = new ListNode(l1.val);
                l1 = l1.next;
            }else{
                cur = cur.next = new ListNode(l2.val);
                l2 = l2.next;
            }
        }
        if(l1 != null)
            cur.next = l1;
        if(l2 != null)
            cur.next = l2;
        return res.next;
    }
}

23.合并K个升序链表(困难)

class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if(lists.length < 1)
            return null;
        PriorityQueue<ListNode> pq = new PriorityQueue<>(lists.length, (a, b) -> a.val - b.val);
        for(int i = 0; i < lists.length; i++){
            if(lists[i] != null)
                pq.offer(lists[i]);
        }

        ListNode res = new ListNode(0), cur = res;
        while(pq.size() > 0){
            ListNode t = pq.poll();
            cur = cur.next = t;
            if (t.next != null)
                pq.offer(t.next);
        }

        return res.next;
    }
}

141.环形链表(简单)

public class Solution {
    public boolean hasCycle(ListNode head) {
        if(head == null)
            return false;
        ListNode slow = head, quick = head;
        while(quick != null && quick.next != null){
            quick = quick.next.next;
            slow = slow.next;
            if(quick == slow)
                return true;
        }
        return false;
    }
}

142.环形链表 II(中等)

public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head, slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(slow == fast){
                fast = head;
                break;
            }
        }

        if(fast == null || fast.next == null)
            return null;
        while(fast != slow){
            fast = fast.next;
            slow = slow.next;
        }

        return slow;
    }
}

876.链表的中间结点(简单)

class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode slow, fast = slow = head;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
}

160.相交链表(简单)

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode la = headA, lb = headB;
        while(la != lb){
            if(la == null)
                la = headB;
            else
                la = la.next;
            if(lb == null)
                lb = headA;
            else
                lb = lb.next;
        }
        return la;
    }
}

19.删除链表的倒数第 N 个结点(中等)

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode pre = new ListNode(0);
        pre.next = head;
        ListNode t = findN(pre, n + 1);
        t.next = t.next.next;

        return pre.next;
    }

    public ListNode findN(ListNode head, int n){
        ListNode pre = head, cur = head;
        for(int i = 0; i < n; i++){
            cur = cur.next;
        }

        while(cur != null){
            cur = cur.next;
            pre = pre.next;
        }

        return pre;
    }
}

第2天

剑指 Offer 24. 反转链表

class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null)
            return head;
        ListNode res = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return res;
    }
}

92. 反转链表 II

class Solution {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        if(left == 1)
            return reverseTo(head, right);
        else
            head.next = reverseBetween(head.next, left - 1, right - 1);
        return head;
    }

    public ListNode reverseTo(ListNode head, int right){
        if(right == 1)
            return head;
        ListNode res = reverseTo(head.next, right - 1);
        
        ListNode next = head.next;
        head.next = next.next;
        next.next = head;

        return res;
    }
}

第3天

25. K 个一组翻转链表

class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode a, b;
        a = b = head;
        for(int i = 0; i < k; i++){
            if(b == null)
                return head;
            b = b.next;
        }
        ListNode res = reverseAToB(a, b);

        a.next = reverseKGroup(b, k);
        return res;
    }

    public ListNode reverseAToB(ListNode a, ListNode b){
        ListNode pre = null, cur = a;
        
        while(cur != b){
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }

        return pre;
    }
}

第4天

234. 回文链表

class Solution {
    ListNode left;
    public boolean isPalindrome(ListNode head) {
        left = head;
        return isRev(head);
    }

    boolean isRev(ListNode head){
        if(head == null)
            return true;
        boolean r = isRev(head.next);
        r = r && left.val == head.val;
        left = left.next;
        return r;
    }
}
class Solution {
    public boolean isPalindrome(ListNode head) {
        ListNode fast, slow;
        fast = slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }

        if(fast != null)
            slow = slow.next;

        ListNode right = reverse(slow);

        ListNode left = head;
        while(right != null){
            if(left.val != right.val)
                return false;
            right = right.next;
            left = left.next;
        }

        return true;
    }

    ListNode reverse(ListNode head){
        ListNode pre = null, cur = head;
        while(cur != null){
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }

        return pre;
    }
}