Leetcode - Insertion Sort List

My code:

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode insertionSortList(ListNode head) {
            if (head == null)
                return null;
            else if (head.next == null)
                return head;
            ListNode dummy = new ListNode(Integer.MIN_VALUE);
            dummy.next = head;
            ListNode temp = head.next;
            ListNode preTemp = head;
            while (preTemp.next != null) {
                ListNode fakeHead = dummy.next;
                ListNode pre = dummy;
                ListNode curr = temp;
                while (fakeHead.val < curr.val && fakeHead != curr) {
                    fakeHead = fakeHead.next;
                    pre = pre.next;
                }
                if (fakeHead != curr) {
                    preTemp.next = temp.next;
                    temp.next = fakeHead;
                    pre.next = curr;
                    temp = preTemp;
                }
                preTemp = temp;
                temp = preTemp.next;
            }
            return dummy.next;
        }
        
        public static void main(String[] args) {
            Solution test = new Solution();
            ListNode n1 = new ListNode(1);
            ListNode n2 = new ListNode(2);
            n2.next = n1;
            System.out.println(test.insertionSortList(n2).val);
        }
    }
Paste_Image.png

這道題目不難,但是很煩。自己也沒能做出來。
剛剛才分析過插入排序的,但是一旦涉及到鏈表,就好復雜。
從第二個結點開始,遍歷之前的子鏈表,如果有值大于這個結點,就將這個結點插入到該值之前。就是 這么個思想。
這里得設置五個結點。
結點1,pre 表示開始遍歷的子鏈表的頭結點的前一個結點。會不斷變化,是fakeHead的上一個結點。
結點2,fakeHead 表示子鏈表的頭結點。
注意,這里fakeHead = dummy.next 而不是 head.next,因為頭結點head一直在變化,只有dummy.next 指向的才是真正的鏈表的頭結點。
結點3,temp 子鏈表右邊一個的結點,用來被判斷插入還是不插入。
結點4,preTemp temp的上一個結點
結點5,curr 用來暫存temp, 這個結點現在想來完全不需要,可以直接用temp,
因為在整個遍歷過程中,temp, preTemp 都是不變的,
遍歷的是之前的子鏈表,變化的是 fakeHead, pre

然后還要注意的是,當遍歷完了可能會有兩種情況出現。
找到比他小的, 那么,插入進去。此時preTemp是不需要再往后移動一位的。
沒找到,那么,preTemp是需要往后移動一位的。然后會出現一些細節問題。
所以做了處理。
如果插入進去了,就讓temp = preTemp.
然后之后統一執行, preTemp = temp;
如果插入過了,preTemp相當于沒移動。
如果沒插入,那么上面的那個if語句就進不去,那么相當于向右移動了一格。

**
總結: sort list using insertion sort

**

Anyway, Good luck, Richardo!

My code:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode insertionSortList(ListNode head) {
        if (head == null || head.next == null)
            return head;
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode pre = head;
        ListNode curr = head.next;
        while (curr != null) {
            if (curr.val >= pre.val) {
                pre = pre.next;
                curr = curr.next;
            }
            else {
                pre.next = curr.next;
                /** find one place from head to pre to insert current node */
                ListNode tPre = dummy;
                ListNode tCurr = dummy.next;
                while (tCurr != pre.next) {
                    if (tCurr.val < curr.val) {
                        tPre = tPre.next;
                        tCurr = tCurr.next;
                    }
                    else {
                        tPre.next = curr;
                        curr.next = tCurr;
                        break;
                    }
                }
                curr = pre.next;
            }
        }
        return dummy.next;
    }
}

感覺我這次的做法,思路很清晰啊。也基本是一遍過得。
如果發現curr < pre, 那就從鏈表頭結點 [head, pre] 遍歷,找到合適的位置插入進去即可。
沒什么難的。

還是之前的結論。鏈表題目不難。就是煩。Array題目是考智商的,不會就是想不出來。

Anyway, Good luck, Richardo!

My code:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode insertionSortList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode p = head;
        ListNode p_pre = dummy;
        while (p != null) {
            ListNode pre = dummy;
            ListNode curr = pre.next;
            while (curr != p) {
                if (curr.val < p.val) {
                    pre = pre.next;
                    curr = curr.next;
                }
                else {
                    p_pre.next = p.next;
                    pre.next = p;
                    p.next = curr;
                    p = p_pre.next;
                    break;
                }
            }
            if (curr == p) {
                p_pre = p_pre.next;
                p = p.next;
            }
        }
        
        return dummy.next;
    }
}

差不多那樣吧。沒什么難的,就是有點煩。
明天開始做DP了。

Anyway, Good luck, Richardo! -- 08/17/2016

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容