重排链表

重排链表

题目描述

CategoryDifficultyLikesDislikes
algorithmsMedium (65.94%)1513-

给定一个单链表 L **的头节点 head ,单链表 L 表示为:

L0 → L1 → … → Ln - 1 → Ln

请将其重新排列后变为:

L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …

不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

示例 1:

https://pic.leetcode-cn.com/1626420311-PkUiGI-image.png

输入:head = [1,2,3,4]
输出:[1,4,2,3]

示例 2:

https://pic.leetcode-cn.com/1626420320-YUiulT-image.png

输入:head = [1,2,3,4,5]
输出:[1,5,2,4,3]

提示:

  • 链表的长度范围为 [1, 5 * 104]
  • 1 <= node.val <= 1000

求解

方法二:寻找链表中点 + 链表逆序 + 合并链表

注意到目标链表即为将原链表的左半端和反转后的右半端合并后的结果。

这样我们的任务即可划分为三步:

找到原链表的中点(参考「876. 链表的中间结点」)。

我们可以使用快慢指针来 O(N) 地找到链表的中间节点。
将原链表的右半端反转(参考「206. 反转链表」)。

我们可以使用迭代法实现链表的反转。
将原链表的两端合并。

因为两链表长度相差不超过 1,因此直接合并即可。

代码

class Solution
{
public:
    ListNode *reorderList(ListNode *head)
    {
        if (head->next == nullptr)
            return head;
        ListNode *dummy = new ListNode(0, head);
        ListNode *fast = dummy, *slow = dummy;

        while (fast && fast->next)
        {
            slow = slow->next;
            fast = fast->next->next;
        }

        ListNode *reverseHalfHead = reverseList(slow->next);
        slow->next = nullptr;
        ListNode *preHalfHead = head;

        while (preHalfHead && reverseHalfHead)
        {
            ListNode *tmp = reverseHalfHead->next;
            reverseHalfHead->next = preHalfHead->next;
            preHalfHead->next = reverseHalfHead;
            preHalfHead = preHalfHead->next->next;
            reverseHalfHead = tmp;
        }

        return dummy->next;
    }
    ListNode *reverseList(ListNode *head)
    {
        ListNode *cur = head->next;
        head->next = nullptr;
        ListNode *tmp = cur;
        while (cur)
        {
            tmp = cur->next;
            cur->next = head;
            head = cur;
            cur = tmp;
        }
        return head;
    }
};

复杂度分析

  • 时间复杂度:O(N),其中 N 是链表中的节点数。
  • 空间复杂度:O(1)。
重排链表的python实现可以使用栈来实现。首先,将链表中的节点压入栈中,然后按照指定的规则依次从栈中出栈,重新连接节点。 代码示例如下: ```python class Solution(object): def reorderList(self, head): if not head: return stack = [] s = head while s.next: stack.append(s.next) s = s.next s = head n = 0 while stack: if n % 2 == 0: one = stack.pop() else: one = stack.pop(0) one.next = None s.next = one s = s.next n += 1 ``` 这段代码中,首先判断头节点是否为空,然后初始化一个栈,并将链表中的其它节点压入栈中。然后,重新遍历链表,根据指定的规则从栈中出栈,并重新连接节点。 这样就能实现重排链表的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [LeetCode: 重排链表 python实现](https://blog.csdn.net/weixin_43786241/article/details/105752892)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [L2-022 重排链表(Python3)](https://blog.csdn.net/weixin_55730361/article/details/126516893)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泡椒香菜‍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值