[LeetCode] Reorder List

简介: Given a singly linked list L: L0→L1→…→Ln−1→LnL_0→L_1→…→L_{n-1}→L_n, reorder it to: L0→Ln→L1→Ln−1→L2→Ln−2→…L_0→L_n→L_1→L_{n-1}→L_2→L_{n-2}→…You must do this in-place without altering the

Given a singly linked list L: L0L1Ln1Ln,
reorder it to: L0LnL1Ln1L2Ln2

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

解题思路1

首先用一个vector存储所有的结点,然后将倒数第i个结点插入到第i个结点之后。

实现代码1

//Runtime:82 ms
#include <iostream>
#include <vector>
using namespace std;

struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL){}
};

class Solution {
public:
    void reorderList(ListNode *head) {
        if (head == NULL)
        {
            return;
        }
        ListNode *p = head;
        vector<ListNode*> nodes;
        while (p)
        {
            nodes.push_back(p);
            p = p->next;
        }

        int left = 0;
        int right = nodes.size() - 1;
        while (left < right)
        {
            nodes[left]->next = nodes[right];
            nodes[right--]->next = nodes[++left];
        }
        nodes[left]->next = NULL;
    }
};
int main()
{
    ListNode *p1 = new ListNode(1);
    ListNode *p2 = new ListNode(2);
    ListNode *p3 = new ListNode(3);
    ListNode *p4 = new ListNode(4);
    p1->next = p2;
    p2->next = p3;
    p3->next = p4;
    Solution s;
    s.reorderList(p1);

    ListNode *p = p1;
    while (p)
    {
        cout<<p->val<<endl;
        p = p->next;
    }
}

解题思路2

首先把链表从中间分成两部分,然后将后一部分链表反转,最后将两个链表合并。

实现代码2

//Runtime:98 ms
class Solution {
public:
    void reorderList(ListNode *head) {
        if (head == NULL)
        {
            return;
        }
        ListNode* ps = head;
        ListNode* pf = head;
        while (pf->next && pf->next->next)
        {
            ps = ps->next;
            pf = pf->next->next;
        }

        ListNode *p2 = ps->next;
        ps->next = NULL;
        p2 = reverseList(p2);
        head = mergeList(head, p2);
    }

    ListNode* mergeList(ListNode *p1, ListNode *p2)
    {
        ListNode *result = p1;
        ListNode *temp;
        while (p2)
        {
            temp = p2;
            p2 = p2->next;

            temp->next = p1->next;
            p1->next = temp;
            p1 = p1->next->next;
        }

        return result;
    }

    ListNode* reverseList(ListNode *head){
        if (head == NULL)
        {
            return NULL;
        }
        ListNode *p = head->next;
        head->next = NULL;
        while (p)
        {
            ListNode *temp = p;
            p = p->next;

            temp->next = head;
            head = temp;
        }

        return head;
    }
};
目录
相关文章
|
Java
Leetcode 114. Flatten Binary Tree to Linked List
思路也很简单,先把root的左子树(如有)变成单链表 leftlinkedlist,把root的右子树(如有)变成单链表 rightlinkedlist,再把root的右节点变成leftlikedlist,再把rightlinkedlist接到leftlinkedlist后面,代码如下。
45 1
|
C++
Leetcode Copy List with Random Pointer(面试题推荐)
给大家推荐一道leetcode上的面试题,这道题的具体讲解在《剑指offer》的P149页有思路讲解,如果你手头有这本书,建议翻阅。
51 0
Leetcode 19.Remove Nth Node From End of List
删除单链表中的倒数第n个节点,链表中删除节点很简单,但这道题你得先知道要删除哪个节点。在我的解法中,我先采用计数的方式来确定删除第几个节点。另外我在头节点之前额外加了一个节点,这样是为了把删除头节点的特殊情况转换为一般情况,代码如下。
38 0
|
6月前
|
大数据 Java 程序员
「LeetCode合集」链表(List)及经典问题
「LeetCode合集」链表(List)及经典问题
52 0
LeetCode 141. 环形链表 Linked List Cycle
LeetCode 141. 环形链表 Linked List Cycle
LeetCode 237. 删除链表中的节点 Delete Node in a Linked List
LeetCode 237. 删除链表中的节点 Delete Node in a Linked List
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 206. 反转链表 Reverse Linked List
LeetCode 206. 反转链表 Reverse Linked List
LeetCode 19. 删除链表的倒数第N个节点 Remove Nth Node From End of List
LeetCode 19. 删除链表的倒数第N个节点 Remove Nth Node From End of List
|
5月前
|
安全 Java
java线程之List集合并发安全问题及解决方案
java线程之List集合并发安全问题及解决方案
958 1