LeetCode 题解(142): Reorder List

题目:

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}.

题解:

做完In-place的sort list,这题就变得非常easy了。先找到中点,如1->2->3->4->5->6中的3,或1->2->3->4->5中的3,将中点之后的链表reverse,可用循环reverse或递归reverse,上一题已给出解法。之后就是sort list中merge的简化版了。

C++版:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void reorderList(ListNode* head) {
        if(head == NULL)
            return;
        
        int len = 0;
        ListNode* p = head;
        while(p != NULL) {
            p = p->next;
            len++;
        }
        
        int i = 0;
        p = head;
        ListNode* q = p;
        while(i < (len - 1) / 2) {
            q = q->next;
            i++;
        }
        
        q->next = reverseTail(q->next);
        
        ListNode* temp = q->next;
        q->next = NULL;
        q = temp;
        while(q != NULL) {
            temp = p->next;
            p->next = q;
            p = temp;
            temp = q->next;
            q->next = p;
            q = temp;
        }
    }
    
    ListNode* reverseTail(ListNode* head) {
        if(head == NULL || head->next == NULL) return head;
        ListNode* newHead = reverseTail(head->next);
        head->next->next = head;
        head->next = NULL;
        return newHead;
    }
};

Java版:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void reorderList(ListNode head) {
        if(head == null)
            return;
            
        ListNode p = head;
        int len = 0;
        while(p != null) {
            p = p.next;
            len++;
        }
        
        p = head;
        ListNode q = p;
        int i = 0;
        while(i < (len - 1) / 2) {
            q = q.next;
            i++;
        }
        
        q.next = reverseList(q.next);
        ListNode temp = q.next;
        q.next = null;
        q = temp;
        while(q != null) {
            temp = p.next;
            p.next = q;
            p = temp;
            temp = q.next;
            q.next = p;
            q = temp;
        }
    }
    
    public ListNode reverseList(ListNode head) {
        if(head == null)
            return null;
        ListNode pre = head;
        ListNode cur = pre.next;
        while(cur != null) {
            pre.next = cur.next;
            cur.next = head;
            head = cur;
            cur = pre.next;
        }
        
        return head;
    }
}

Python版:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # @param {ListNode} head
    # @return {void} Do not return anything, modify head in-place instead.
    def reorderList(self, head):
        if head == None:
            return
        
        length, p = 0, head
        while p != None:
            p = p.next
            length += 1
            
        p, q, i = head, head, 0
        while i < (length - 1) / 2:
            q = q.next
            i += 1
            
        q.next = self.reverseList(q.next)
        temp = q.next
        q.next = None
        q = temp
        while q != None:
            temp = p.next
            p.next = q
            p = temp
            temp = q.next
            q.next = p
            q = temp
        
    def reverseList(self, head):
        if head == None:
            return None
            
        pre, cur = head, head.next
        while cur != None:
            pre.next = cur.next
            cur.next = head
            head = cur
            cur = pre.next
            
        return head


你可能感兴趣的:(Algorithm,LeetCode,面试题)