[LeetCode#147]Insertion Sort List

The problem:

Sort a linked list using insertion sort.

My analysis:

The idea behind this solution is easy, but the mainipulation over linkedlist is hard and very easy to make errors.

The key idea is that:

we maintain two linkedlists:

1. the sorted linkedlist

2. the remaining linkedlist

each time we insert a listnode in remaining linkedlist into sorted linkedlist. to achieve this goal we need to maintain following information:

In remaining linkedlist:

1. The current node, which is the first node in remaining linkedlist. 

2. The next node, which is the next node(of first node) in remaining linkedlist. Since we might change the next pointer of current node, we should record the information. 

In sorted linkedlist

1. The current comparing node, which is the node used to compare with the current node of remaining linkedlist.

2. The previous node of current comparing node.

3. The last node of current sorted linkedlist. This node is very important, since we might encounter a node which is larger than every node in the sorted linked list. At this time, we need to use last pointer to point it. 



There are two cases in inserting a node into the sorted linkedlist.

1. Find the node that just larger than the current node. 

while (comp.val < cur.val && comp != last) {

    comp = comp.next;

    pre = pre.next;

}

Note: we should not exceed the scope of sorted list, thus we add "comp != last" as checking condition.



2. Insert the node at the right position. 

2.1 insert the current node into the middle of the sorted linked list.

e: 

1 -> 2 -> 3 -> 5 -> 6

1 -> 2 -> 3[pre] -> (4) -> 5[comp] -> 6

pre = 3

comp = 5

4.next = 3.next

3.next = 4.next

no need to update last pointer



2.2 insert the current node at the end of the sorted linked list.

e:

1 -> 2 -> 3 -> 5 -> 6 [last]

1 -> 2 -> 3 -> 5 -> 6 -> 7[last]

6.next(last.next) = 7



Note: the above analysis doest not cover the situation when last node, comparing node, cur node is the same node(when the current node is the )!!! This case would break the invariant and result in circle!



if (cur != head) { 

    if (comp.val >= cur.val) { 

        cur.next = pre.next;

        pre.next = cur;

    } else{

        last.next = cur;

        last = cur;

}



Very important:

The sorted list's head may no longer be the passedin head, we could not use head as the first node in the sorted list. 

we could only get the head through dummy.next.

comp = dummy.next; //the new head may no longer the passed in "head" node any

My first solution:

public class Solution {

    public ListNode insertionSortList(ListNode head) {

        if (head == null)

            return null;

        ListNode dummy = new ListNode(0);

        dummy.next = head;

        ListNode cur = head;

        ListNode comp = head;

        ListNode pre = dummy;

        ListNode last = head; 

        while (cur != null) {

            ListNode next = cur.next;

            while (comp.val < cur.val && comp != last) {

                comp = comp.next;

                pre = pre.next;

            }

            if (cur != head) { //incase of the head node

                if (comp.val >= cur.val) { //this include when comp.val >= cur.val and comp == last

                    cur.next = pre.next;

                    pre.next = cur;

                } else{

                    last.next = cur;

                    last = cur;//the last element in the reshaped LinkedList.

                }

            }

            cur = next;

            pre = dummy;

            comp = dummy.next; //the new head may no longer the passed in "head" node any

            }

        last.next = null;//avoid cycle!

        return dummy.next;

    }

}

An improvement in tackling corner case:

Why not we skip the head, and start fromthe next node of head. Since in any situations, we tackle the first node(head) in the same way.

        ListNode dummy = new ListNode(0);

        dummy.next = head;

        ListNode cur = head.next;

        ListNode comp = head;

        ListNode pre = dummy;

        ListNode last = head; 

My solution:

public class Solution {

    public ListNode insertionSortList(ListNode head) {

        if (head == null)

            return null;

        ListNode dummy = new ListNode(0);

        dummy.next = head;

        ListNode cur = head.next;

        ListNode comp = head;

        ListNode pre = dummy;

        ListNode last = head; 

        while (cur != null) {

            ListNode next = cur.next;

            while (comp.val < cur.val && comp != last) {

                comp = comp.next;

                pre = pre.next;

            }

            if (comp.val >= cur.val) { //this include when comp.val >= cur.val and comp == last

                cur.next = pre.next;

                pre.next = cur;

            } else{

                last.next = cur;

                last = cur;//the last element in the reshaped LinkedList.

            }

            cur = next;

            pre = dummy;

            comp = dummy.next; //the new head may no longer the passed in "head" node any

            }

        last.next = null;//avoid cycle!

        return dummy.next;

    }

}

 

你可能感兴趣的:(LeetCode)