-
Notifications
You must be signed in to change notification settings - Fork 0
/
0092.ReverseLinkedListII.js
93 lines (71 loc) · 2.12 KB
/
0092.ReverseLinkedListII.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.
//
// Example 1:
// Input: head = [1,2,3,4,5], left = 2, right = 4
// Output: [1,4,3,2,5]
// Example 2:
// Input: head = [5], left = 1, right = 1
// Output: [5]
//
// Constraints:
// The number of nodes in the list is n.
// 1 <= n <= 500
// -500 <= Node.val <= 500
// 1 <= left <= right <= n
//
// Follow up: Could you do it in one pass?
// 来源:力扣(LeetCode)
// 链接:https://leetcode-cn.com/problems/reverse-linked-list-ii
// 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
// 🎨 方法一:穿针引线
// 📝 思路:找到 left right,截断链表,翻转链表,穿针引线
/**
* @param {ListNode} head
* @param {number} left
* @param {number} right
* @return {ListNode}
*/
var reverseBetween = function (head, left, right) {
// 虚拟头节点
const dummyNode = new ListNode(null);
dummyNode.next = head
// left 的前驱:从dummyNode 向前走 left - 1 步
let pre = dummyNode;
for (let i = 0; i < left - 1; i++) {
pre = pre.next
}
// right 节点:从 pre 向前走 right - (left -1 ) 步
let rightNode = pre;
for (let i = 0; i < right - left + 1; i++) {
rightNode = rightNode.next
}
// leftNode 和 rightNode 的后继
let leftNode = pre.next
let curr = rightNode.next
// 截断链表
pre.next = null;
rightNode.next = null
// 反转链表:同第 206 题
reverseLinkedList(leftNode);
// 穿针引线
pre.next = rightNode
leftNode.next = curr
return dummyNode.next
};
function reverseLinkedList(head) {
let pre = null;
let cur = head;
while (cur) {
const next = cur.next
cur.next = pre
pre = cur
cur = next
}
}