-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0092-reverseBetween.WIP.ts
More file actions
39 lines (33 loc) · 869 Bytes
/
0092-reverseBetween.WIP.ts
File metadata and controls
39 lines (33 loc) · 869 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
36
37
38
39
import type { ListNode } from '../utils/tree'
/**
* 92. Reverse Linked List II
* {@link https://leetcode.com/explore/interview/card/leetcodes-interview-crash-course-data-structures-and-algorithms/704/linked-lists/4598/ | Link}
*
* TODO: add handling for the boundary inputs
*
*/
export function reverseBetween(
head: ListNode | null,
left: number,
right: number
): ListNode | null {
let node = head
let reversed: ListNode | null = null
let listLeft: ListNode | null = null
let reverseLast: ListNode | null = null
let c = 1
while (node && c <= right) {
const next = node.next
if (c === left - 1) listLeft = node
if (c >= left) {
node.next = reversed
reversed = node
if (c === left) reverseLast = reversed
}
node = next
c++
}
listLeft!.next = reversed
reverseLast!.next = node
return head
}