【Leetcode】143. Reorder List

於淼發表於2018-02-27

Question:

Given a singly linked list L: L0L1→…→Ln-1Ln,
reorder it to: L0LnL1Ln-1L2Ln-2→…

You must do this in-place without altering the nodes' values.

For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.

Tips:
給定一個單連結串列,將連結串列重新排序,注意不能改變結點的值。
排序規則如下:
L0L1→…→Ln-1Ln,
L0LnL1Ln-1L2Ln-2→…
思路:
重新排序後的連結串列,前1/2 結點相對順序不變,而後半部分是逆序。所以我的思路是先將後半部分結點翻轉,變為逆序,再將後半部分結點依次插入到前半部分中去。
大致分為三部分:
(1)找到連結串列的中間位置,將連結串列分為兩部分。
(2)將第二部分連結串列逆序
(3)將第二部分所有節點依次插入到前半部分結點之間。
程式碼:
public void reorderList(ListNode head) {
        if (head == null || head.next == null)
            return;
        // Find the part2;第二部分是從slow.next開始的
        ListNode slow = head;
        ListNode fast = head;
        while (fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        System.out.println("slow"+slow.val);
        ListNode mid = slow.next;
        slow.next = null;
        System.out.println("mid"+mid.val);
        // 將第二部分翻轉;
        ListNode pre = null;
        ListNode cur = mid;
        while (cur != null) {
            if (cur.next != null) {
                ListNode next = cur.next;
                System.out.println("next"+next.val);
                cur.next = pre;
                pre = cur;
                cur = next;
            } else {
                cur.next = pre;
                pre = cur;
                cur=null;
            }
        }
        System.out.println("pre"+pre.val);
        // append one by one;
        ListNode p1 = head;
        ListNode p2 = pre;
        while (p2 != null) {
            ListNode n1 = p1.next;
            ListNode n2 = p2.next;
            p1.next = p2;
            p2.next = n1;
            p1 = p1.next.next;
            p1 = n1;
            p2 = n2;
        }    
        //print
        while (head != null) {
            System.out.println(head.val);
            head = head.next;
        }
    }

程式碼中的一些輸出 是為了驗證結果的正確性 提交時可刪除。leetcode提交版版程式碼如下:

public void reorderList(ListNode head) {
        if (head == null || head.next == null)
            return;
        // Find the part2;第二部分是從slow.next開始的
        ListNode slow = head;
        ListNode fast = head;
        while (fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode mid = slow.next;
        slow.next = null;
        // 將第二部分翻轉;
        ListNode pre = null;
        ListNode cur = mid;
        while (cur != null) {
            if (cur.next != null) {
                ListNode next = cur.next;
                cur.next = pre;
                pre = cur;
                cur = next;
            } else {
                cur.next = pre;
                pre = cur;
                cur=null;
            }
        }
        // append one by one;
        ListNode p1 = head;
        ListNode p2 = pre;
        while (p2 != null) {
            ListNode n1 = p1.next;
            ListNode n2 = p2.next;
            p1.next = p2;
            p2.next = n1;
            p1 = p1.next.next;
            p1 = n1;
            p2 = n2;
        }
    }

 

相關文章