LinkedList

206. Reverse Linked List

    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curt = head;
        while (curt != null) {
            ListNode next = curt.next;
            curt.next = prev;
            prev = curt;
            curt = next;
        }
        return prev;
    }

Recursion

    ListNode newhead = null;
    public ListNode reverseList(ListNode head) {
        if (head == null) return null;
        helper(head);
        return newhead;
    }
    
    private ListNode helper(ListNode curt) {
        if (curt.next == null) {
            newhead = curt;
            return curt;
        }
        ListNode next = helper(curt.next);
        curt.next.next = curt;
        curt.next = null;
        return next;
    }

Use a curt pointer and a nxt pointer to move from head. Detect if curt.val == nxt.val then remove nxt node.

21. Merge Two Sorted Lists

Use dummy node, and a current node starts at dummy. While the first and second list is not null, add the smaller node and move forward.

When end, detect the unfinished list, add to the end.

Iteration T: O(n + m) S: O(1)

Recursion T: O(n + m) S: O(m + n)

  1. Divide and Conquer, Merge sort T: O(n*logk) S: O(1)

2. PQ T: O(nlogk) S: O(n)

Construct node and take carry.

T: O(max(m, n)) S: O(max(m, n))

Use two stacks to store two lists separately. Pop node and construct new node, insert between dummy and dummy.next.

141. Linked List Cycle

24. Swap Nodes in Pairs

92. Reverse Linked List II

  • 记录一个pre,把pre移动到开始reverse的前面

  • pre不动,把后面的reverse,把next接在pre和pre.next 中间

  • Return dummy.next

T: O(n) S: O(1) in-place

19. Remove Nth Node From End of List

  • 一个fast, 一个slow 都从dummy 走

  • Fast走 i = 0 : n。这样fast和尾的距离是total - n,

  • slow,fast同时走,fast是null的时候,slow走了total -n ,slow和尾的距离是n

  • 删除slow后面节点

83. Remove Duplicates from Sorted List

一个指针,如果curt.val == curt.next.val 情况下curt.next = curt.next.next

82. Remove Duplicates from Sorted List II

  • 两个指针走,在curt.next.val == curt.val情况下移动curt

  • 如果pre的下一个不是curt,说明有duplicate,

  • 否则同时移动pre和curt

203. Remove Linked List Elements

Curt从dummy走,在curt.next.val == val的情况下,curt.next = curt.next.next

369. Plus One Linked List

这个题要说把linkedlist最后一个节点+1。如果有进位的话加到前面那个node。这样对于单向列表很麻烦,用recursion做非常直观。

  • 如果碰到null,return 1,因为最后一位+1;

  • 加上carry之后如果大于9,reassign value.返回1

  • 否则返回0

  • 这样的话有可能多出来一个node,用dummy来处理

160. Intersection of Two Linked Lists

先把两个list的长度求出来,减少长的那个,这样如果他们相等,就代表他们到最后的距离是相等的。这样说明两个head继续走,走到他们相等的时候就return。否则return null.

Same method as 141, 142.

Use a Hash-map to store original and it is corresponding new node. Loop over it and set the random pointer.

Add the new node next to the original node. For example 1 -> 1' -> 2 -> 2' -> 3 -> 3' -> null

234. Palindrome Linked List

Palindrome特性是从中间到两边对称。一般Palindrome都涉及到单双数的问题。这个题也是找中点,然后从中点往两边比较是不是Palindrome。

  • 找中点用快慢指针, while (fast.next != null && fast.next.next != null)

  • Fast到头之后,check一下fast是不是null,如果不是,说明是even,slow还要往前走一个。

  • 这样从slow到尾reverse()。用reverse LinkedList变成这样:

  • 最后从head和slow 走,看看是不是相等

86. Partition List

143. Reorder List

从 1->2->3->4, reorder it to 1->4->2->3。这样可以把list分成左右两段,反转后面的,然后相互插入形成结果。

  • 快慢指针找中点,把slow记录一下成为preMiddle,然后slow = slow.next;

  • 这样preMiddle后面的,也就是从slow开始,都要reverse。就是把slow.next都插入到preMiddle.next。

  • 然后再互相merge

148. Sort List

这题一看就是merge sort。跟传统merge sort一样,不过最后要merge两个list。还是先快慢指针分一半。用一个pre记录slow之前的点,然后把pre.next = null;这样recursively两边再分,最后merge。 Merge时候注意null pointer

61. Rotate List

这个题是说把后面数k个Node接到前面去,有个情况就是k会比总长度大。k % length == 0 说明不用rotate。这种情况用k % length就会得到从前往后要多少个接到前面, length - k % length是从后往前有多少接到前面。

先一个指针找长度,另外一个指针走length - k % length,把两个指针中间接到前面去。(两个指针都从dummy 走)

Use a range pointer to detect if the range is out of scope. Reverse the part next to pre for k - 1 times.

1669. Merge In Between Linked Lists

Last updated

Was this helpful?