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 <= 500
1 <= left <= right <= n
进阶: 你可以使用一趟扫描完成反转吗?
方法一:模拟
定义一个虚拟头结点 dummy
,指向链表的头结点 head
,然后定义一个指针 pre
指向 dummy
,从虚拟头结点开始遍历链表,遍历到第 left
个结点时,将 pre
指向该结点,然后从该结点开始遍历 right - left + 1
次,将遍历到的结点依次插入到 pre
的后面,最后返回 dummy.next
即可。
时间复杂度
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