-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathlinked_list_reverse.js
47 lines (42 loc) · 1.33 KB
/
linked_list_reverse.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
//Problem statement-Given the head of a singly linked list, reverse the list, and return the reversed list.
// Iteratively
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function(head) {
// Handling the edge cases if head node is null or when single node is there
if (head === null || head.next === null) {
return head;
}
// Using three pointer approach
let prev = null;
let curr = head;
let fwd = null;
while (curr !== null) {
// Updatng the value of each of the pointers
fwd = curr.next;
curr.next = prev;
prev = curr;
curr = fwd;
}
// Returning the head of the reversed linked list
return prev;
};
// Time complexity - O(N)
// Space complexity - O(1)
// Recursively
var reverseList = function(head) {
// Base case- If the head is null or single node is there
if (head === null || head.next === null) {
return head;
}
// Recursively reverse the remaining list starting from the next node
const reversedList = reverseList(head.next);
// Reversing the links between the current and next node
head.next.next = head;
head.next = null;
// Returning the head of the reversed linked list
return reversedList;
};
// Time complexity - O(N)
// Space complexity - O(N)