💻 在线刷题 · 全屏 IDE 模式进入刷题模式 →
92. 反转链表 II
题目描述
给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。
示例 1:
输入:head = [1,2,3,4,5], left = 2, right = 4 输出:[1,4,3,2,5]

示例 2:
输入:head = [5], left = 1, right = 1 输出:[5]
提示:
- 链表中节点数目为
n 1 <= n <= 500-500 <= Node.val <= 5001 <= left <= right <= n
进阶: 你可以使用一趟扫描完成反转吗?
方法一:模拟
定义一个虚拟头结点 dummy,指向链表的头结点 head,然后定义一个指针 pre 指向 dummy,从虚拟头结点开始遍历链表,遍历到第 left 个结点时,将 pre 指向该结点,然后从该结点开始遍历 right - left + 1 次,将遍历到的结点依次插入到 pre 的后面,最后返回 dummy.next 即可。
时间复杂度
可视化演示
以
head = [1, 2, 3, 4, 5]、left = 2、right = 4为例,演示区间反转:pre从虚拟头节点走到第left-1个节点,循环将cur依次反接到pre之前。蓝色为当前待反接节点,黄色为t(后继节点),绿色为已完成反转的节点,红色为反转区间。点击 ▶ 播放,或逐步操作。
链表
→
11
→
22
→
33
→
44
→
55
→
当前操作比较/参照已完成已连接目标/结果空节点
head = [1,2,3,4,5],left = 2,right = 4。创建虚拟头节点 dummy,pre = dummy。
1 / 10
java
class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
if (head.next == null || left == right) {
return head;
}
ListNode dummy = new ListNode(0, head);
ListNode pre = dummy;
for (int i = 0; i < left - 1; ++i) {
pre = pre.next;
}
ListNode p = pre;
ListNode q = pre.next;
ListNode cur = q;
for (int i = 0; i < right - left + 1; ++i) {
ListNode t = cur.next;
cur.next = pre;
pre = cur;
cur = t;
}
p.next = pre;
q.next = cur;
return dummy.next;
}
}cpp
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int left, int right) {
if (!head->next || left == right) {
return head;
}
ListNode* dummy = new ListNode(0, head);
ListNode* pre = dummy;
for (int i = 0; i < left - 1; ++i) {
pre = pre->next;
}
ListNode *p = pre, *q = pre->next;
ListNode* cur = q;
for (int i = 0; i < right - left + 1; ++i) {
ListNode* t = cur->next;
cur->next = pre;
pre = cur;
cur = t;
}
p->next = pre;
q->next = cur;
return dummy->next;
}
};ts
function reverseBetween(head: ListNode | null, left: number, right: number): ListNode | null {
const n = right - left;
if (n === 0) {
return head;
}
const dummy = new ListNode(0, head);
let pre = null;
let cur = dummy;
for (let i = 0; i < left; i++) {
pre = cur;
cur = cur.next;
}
const h = pre;
pre = null;
for (let i = 0; i <= n; i++) {
const next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
h.next.next = cur;
h.next = pre;
return dummy.next;
}python
class Solution:
def reverseBetween(
self, head: Optional[ListNode], left: int, right: int
) -> Optional[ListNode]:
if head.next is None or left == right:
return head
dummy = ListNode(0, head)
pre = dummy
for _ in range(left - 1):
pre = pre.next
p, q = pre, pre.next
cur = q
for _ in range(right - left + 1):
t = cur.next
cur.next = pre
pre, cur = cur, t
p.next = pre
q.next = cur
return dummy.next