Saturday, June 7, 2014

Insertion Sort List

I wrote 2 solutions for this problem. Solution 1 can get correct result, but TLE. I am not sure why this happens. Since time complexity of the code is O(n^2),  it is supposed to be easier to pass test cases. One guy said TLE might be due to that algorithm generates circular list, but I can not agree with him, because each time a node is inserted or moved, the next of node is definitely "pointed" to some certain node.

Solution 2 is accepted. The only difference is that solution 2 creates a different list, not manipulates with original list.

Now based on my experience solving Leetcode problems, I think sometimes Leetcode OJ does not fully check all the restrictions described in question. Some fake answers might get accepted as well. Thus the AC rate might not be a good criteria to determine this question is difficult or not.

Alright, lets check the code out.
//Solution 1, but TLE
public ListNode insertionSortList(ListNode head) {
     
        if(head == null || head.next == null) return head;
     
        ListNode crntPre = head;
        ListNode crntNode = crntPre.next;
        // make sure that first 2 elements are sorted    
        if(crntPre.val > crntNode.val){//i.e. swap head and head.next
            crntPre.next = crntNode.next;
            crntNode.next = crntPre;
            head = crntNode;
        }
        crntPre = crntNode;
        crntNode = crntNode.next;
        while(crntNode != null){
            ListNode tmpNode = head;
            ListNode tmpPre = null;
            while(tmpNode != crntNode && crntNode.val >= tmpNode.val ){
                tmpPre = tmpNode;
                tmpNode = tmpNode.next;
            }
         
            if(tmpPre == null){// crntNode.val < head.val
                crntPre.next = crntNode.next;
                crntNode.next = head;
                head = crntNode;              
            }
            else{

                if(crntNode.val < tmpNode.val){// crntNode.val < tmpNode.val, insert crntNode before tmpNode
                    crntPre.next = crntNode.next;
                    crntNode.next = tmpNode;
                    tmpPre.next = crntNode;
                 
                }
                //else, crntNode.val is larger than all nodes before it, no need for any change
            }
            crntPre = crntNode;
            crntNode = crntNode.next;
        }
     
        return head;
}


// Solution 2, accpeted

public ListNode insertionSortList(ListNode head) {
        // Solution 2
        if(head == null || head.next == null) return head;
     
        ListNode newHead = new ListNode(head.val);
     
        ListNode crntNode = head.next;

        while(crntNode != null){
             
            ListNode tmpNode = newHead;
            ListNode tmpPre = null;
            ListNode newNode = new ListNode(crntNode.val);
         
            while(tmpNode!=null && tmpNode.val <= crntNode.val){// any differecen between < and <= ??
                tmpPre = tmpNode;
                tmpNode = tmpNode.next;
            }
            if(tmpPre == null){// smaller than newHead
             
                newNode.next = newHead;
                newHead = newNode;
            }
            else{
                if(tmpNode == null){// crntNode.val is larger than all nodes of newHead
                    tmpPre.next = newNode;
                }
                else{
                    newNode.next = tmpNode;
                    tmpPre.next = newNode;
                }
            }
         
            crntNode = crntNode.next;
        }
     
        return newHead;
    }
}

No comments:

Post a Comment