143. Reorder List**

143. Reorder List**

https://leetcode.com/problems/reorder-list/

Title Description

Given a singly linked list L: L0 -> L1 -> … -> Ln-1 -> Ln,
reorder it to: L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 -> …

You may not modify the values in the list’s nodes, only nodes itself may be changed.

Example 1:

Given 1->2->3->4, reorder it to 1->4->2->3.

Example 2:

Given 1->2->3->4->5, reorder it to 1->5->2->4->3.

C ++ implementation 1

The basic idea:

  1. First, find the midpoint of the list, the list is divided into the first half and second half of the pre post
  2. Flip the second half post
  3. And a rear half portion of the inverted pre post pre-merger

Find the midpoint of the list can use the pointer speed.

class Solution {
private:
    ListNode* reverse(ListNode *head) {
        ListNode *prev = nullptr;
        while (head) {
            auto tmp = head->next;
            head->next = prev;
            prev = head;
            head = tmp;
        }
        return prev;
    }
public:
    void reorderList(ListNode* head) {
        if (!head || !head->next) return;
        // 第一步
        ListNode *slow = head, *fast = head;
        while (fast && fast->next) {
            slow = slow->next;
            fast = fast->next->next;
        }
        auto rlist = slow->next;
        slow->next = nullptr;
        // 第二步
        rlist = reverse(rlist);
        // 第三步
        ListNode *dummy = new ListNode(0);
        auto p = dummy;
        bool choose = true;
        while (head && rlist) {
            if (choose) {
                p->next = head;
                head = head->next;
            } else {
                p->next = rlist;
                rlist = rlist->next;
            }
            p = p->next;
            choose = !choose;
        }
        p->next = head ? head : rlist;
        head = dummy->next;
    }
};

2 in C ++

The following code mergeoperation savored be found. headPoint to the whole list, but ptrthe second half after the list of points to flip, write the code no problem with the idea a bit like:. 160. The Intersection of Two Linked Lists * .

class Solution {
private:
    ListNode* reverse(ListNode *head) {
        ListNode *prev = nullptr;
        while (head) {
            auto tmp = head->next;
            head->next = prev;
            prev = head;
            head = tmp;
        }
        return prev;
    }

    void merge(ListNode *l1, ListNode *l2) {
        for (auto p1 = l1, p2 = l2; p1; ) {
            auto t = p1->next;
            p1->next = p2;
            p1 = p1->next;
            p2 = t;
        }
    }
public:
    void reorderList(ListNode* head) {
        if (!head || !head->next)
            return;

        ListNode *slow = head, *fast = head->next;
        while (fast && fast->next) {
            slow = slow->next;
            fast = fast->next->next;
        }
        auto ptr = reverse(slow->next);
        merge(head, ptr);
    }
};
Published 455 original articles · won praise 8 · views 20000 +

Guess you like

Origin blog.csdn.net/Eric_1993/article/details/105001193