-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse-linked-list.js
67 lines (56 loc) · 1.2 KB
/
reverse-linked-list.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
/*
Reverse A Linked List (https://leetcode.com/problems/reverse-linked-list/)
- Given a linked list, return it in reverse.
Constraints:
- Return null when receiving null
- Return the same node when receiving a single node
*/
// ---- Test Cases ----
// 1 -> 2 -> 3 -> 4 -> 5 -> null
// Should return: 5 -> 4 -> 3 -> 2 -> 1 -> null
const linkedList1 = {
val: 1,
next: {
val: 2,
next: {
val: 3,
next: {
val: 4,
next: {
val: 5,
next: null,
},
},
},
},
};
// 3 -> null
// Should return: 3 -> null
const linkedList2 = {
val: 3,
next: null,
};
// null
// Should return: null
const linkedList3 = null;
// ---- Solution ----
const reverseLinkedList = function (head) {
if (!head || !head.next) return head;
let currentNode = head;
let prevNode = null;
while (currentNode) {
const nextNode = currentNode.next;
currentNode.next = prevNode;
prevNode = currentNode;
currentNode = nextNode;
}
return prevNode;
};
console.log(reverseLinkedList(linkedList1));
console.log(reverseLinkedList(linkedList2));
console.log(reverseLinkedList(linkedList3));
// ---- Space and Time Complexity ----
/*
Time: O(n)
Space: O(1)
*/