-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsolution.ts
48 lines (40 loc) · 839 Bytes
/
solution.ts
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
/*
* @lc app=leetcode id=206 lang=javascript
*
* [206] Reverse Linked List
*/
// @lc code=start
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
interface ListNode {
val: number;
next: ListNode | null;
}
/**
* @param {ListNode} head
* @return {ListNode}
*/
const reverseList = (head: ListNode | null): ListNode | null => {
// * ['48 ms', '98.66 %', '34.9 MB', '73.91 %']
// * null cur -> next -> ...
// * prev cur next
// * prev <- cur next
// * prev cur next
let cur = head;
let prev: ListNode | null = null;
let next: ListNode | null;
while (cur !== null) {
next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
return prev;
};
// @lc code=end
export { reverseList };