-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path92-Reverse-Linked-List-II.js
More file actions
35 lines (30 loc) · 975 Bytes
/
92-Reverse-Linked-List-II.js
File metadata and controls
35 lines (30 loc) · 975 Bytes
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
/* eslint-disable no-undef */
// 92. Reverse Linked List II [Medium]
// https://leetcode.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)
* }
*/
/**
* @param {ListNode} head
* @param {number} left
* @param {number} right
* @return {ListNode}
*/
const reverseBetween = (head, left, right) => {
const swapValues = (node1, node2) => ([node1.val, node2.val] = [node2.val, node1.val]);
const swapCandidates = [];
let currentNode = head;
for (let i = 1; i <= right; i++) {
if (i >= left) swapCandidates.push(currentNode);
currentNode = currentNode.next;
}
const middleIdx = Math.floor(swapCandidates.length / 2);
for (let i = 0; i < middleIdx; i++) {
swapValues(swapCandidates[i], swapCandidates[swapCandidates.length - 1 - i]);
}
return head;
};