LeetCode 题解工作台
重排链表
给定一个单链表 L 的头节点 head ,单链表 L 表示为: L 0 → L 1 → … → L n - 1 → L n 请将其重新排列后变为: L 0 → L n → L 1 → L n - 1 → L 2 → L n - 2 → … 不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。…
4
题型
8
代码语言
3
相关题
当前训练重点
中等 · 链表指针操作
答案摘要
我们先用快慢指针找到链表的中点,然后将链表的后半部分反转,最后将左右两个链表合并。 时间复杂度 ,其中 是链表的长度。空间复杂度 。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 链表指针操作 题型思路
题目描述
给定一个单链表 L 的头节点 head ,单链表 L 表示为:
L0 → L1 → … → Ln - 1 → Ln
请将其重新排列后变为:
L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …
不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
示例 1:

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

输入:head = [1,2,3,4,5] 输出:[1,5,2,4,3]
提示:
- 链表的长度范围为
[1, 5 * 104] 1 <= node.val <= 1000
解题思路
方法一:快慢指针 + 反转链表 + 合并链表
我们先用快慢指针找到链表的中点,然后将链表的后半部分反转,最后将左右两个链表合并。
时间复杂度 ,其中 是链表的长度。空间复杂度 。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
# 快慢指针找到链表中点
fast = slow = head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
# cur 指向右半部分链表
cur = slow.next
slow.next = None
# 反转右半部分链表
pre = None
while cur:
t = cur.next
cur.next = pre
pre, cur = cur, t
cur = head
# 此时 cur, pre 分别指向链表左右两半的第一个节点
# 合并
while pre:
t = pre.next
pre.next = cur.next
cur.next = pre
cur, pre = pre.next, t
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(n) because each node is visited a constant number of times during split, reverse, and merge. Space complexity is O(1) for in-place reversal and merging, though recursion or a stack can increase space usage if used. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Check if the candidate can split the list efficiently without extra space.
- question_mark
Watch for correct in-place reversal of the second half.
- question_mark
Ensure merging maintains the alternating pattern without node loss.
常见陷阱
外企场景- error
Modifying node values instead of pointers, which violates the problem constraints.
- error
Losing reference to the second half while splitting, leading to memory leaks.
- error
Incorrect merging order causing cycles or skipped nodes.
进阶变体
外企场景- arrow_right_alt
Reorder list using recursion instead of iterative merge.
- arrow_right_alt
Reorder list using a stack to store the second half for interleaving.
- arrow_right_alt
Modify the pattern to L0 → Ln → L1 → Ln-1 → L2 → ... only for odd-length lists.