Skip to content

Commit 40b5c6e

Browse files
authored
Update 92-reverse-linked-list-ii.js
1 parent de9d4d2 commit 40b5c6e

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

92-reverse-linked-list-ii.js

+34
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,40 @@ const reverseBetween = function(head, m, n) {
8484
return dummy.next;
8585
};
8686

87+
// another
88+
89+
/**
90+
* Definition for singly-linked list.
91+
* function ListNode(val, next) {
92+
* this.val = (val===undefined ? 0 : val)
93+
* this.next = (next===undefined ? null : next)
94+
* }
95+
*/
96+
/**
97+
* @param {ListNode} head
98+
* @param {number} left
99+
* @param {number} right
100+
* @return {ListNode}
101+
*/
102+
const reverseBetween = function(head, left, right) {
103+
if(head == null || left === right) return head
104+
const dummy = new ListNode()
105+
dummy.next = head
106+
let tail = null, p = dummy
107+
for(let i = 1; i < left; i++) {
108+
p = p.next
109+
}
110+
tail = p.next
111+
let tmp
112+
for(let i = 0; i < right - left; i++) {
113+
tmp = p.next
114+
p.next = tail.next
115+
tail.next = tail.next.next
116+
p.next.next = tmp
117+
}
118+
119+
return dummy.next
120+
};
87121

88122
// another
89123

0 commit comments

Comments
 (0)