Skip to content

Commit 491f01a

Browse files
authored
refactor: js | linkedlist (#770)
1 parent 23b6d27 commit 491f01a

12 files changed

+732
-359
lines changed

Diff for: javascript/138-Copy-List-with-Random-Pointer.js

+58-13
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,65 @@
11
/**
2+
* https://leetcode.com/problems/copy-list-with-random-pointer/
3+
* Time O(N) | Space O(N)
24
* @param {Node} head
35
* @return {Node}
46
*/
5-
var copyRandomList = function (head) {
6-
let map = new Map();
7-
let ptr = head;
8-
9-
while (ptr) {
10-
// map old - new
11-
map.set(ptr, new Node(ptr.val, null, null));
12-
ptr = ptr.next;
7+
var copyRandomList = function(head, map = new Map()) {
8+
if (!head) return null;
9+
if (map.has(head)) return map.get(head);
10+
11+
const clone = new Node(head.val);
12+
13+
map.set(head, clone); /* | Space O(N) */
14+
clone.next = copyRandomList(head.next, map); /* Time O(N) | Space O(N) */
15+
clone.random = copyRandomList(head.random, map);/* Time O(N) | Space O(N) */
16+
17+
return clone;
18+
}
19+
20+
/**
21+
* https://leetcode.com/problems/copy-list-with-random-pointer/
22+
* Time O(N) | Space O(1)
23+
* @param {Node} head
24+
* @return {Node}
25+
*/
26+
var copyRandomList = function(head) {
27+
if (!head) return null;
28+
29+
cloneNode(head); /* Time O(N) */
30+
connectRandomNode(head); /* Time O(N) */
31+
32+
return connectNode(head);/* Time O(N) */
33+
};
34+
35+
const cloneNode = (curr) => {
36+
while (curr) { /* Time O(N) */
37+
const node = new Node(curr.val);
38+
39+
node.next = curr.next;
40+
curr.next = node;
41+
curr = node.next;
1342
}
1443

15-
for (const [oldptr, newptr] of map) {
16-
newptr.next = oldptr.next && map.get(oldptr.next);
17-
newptr.random = oldptr.random && map.get(oldptr.random);
44+
return curr;
45+
}
46+
47+
const connectRandomNode = (curr) => {
48+
while (curr) { /* Time O(N) */
49+
curr.next.random = curr.random?.next || null;
50+
curr = curr.next.next;
1851
}
19-
return map.get(head);
20-
};
52+
}
53+
54+
const connectNode = (head) => {
55+
let [ prev, curr, next ] = [ head, head.next, head.next ];
56+
57+
while (prev) { /* Time O(N) */
58+
prev.next = prev.next.next;
59+
curr.next = curr?.next?.next || null;
60+
prev = prev.next;
61+
curr = curr.next;
62+
}
63+
64+
return next
65+
}

Diff for: javascript/141-Linked-List-Cycle.js

+26-15
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
/**
2-
* Definition for singly-linked list.
3-
* function ListNode(val) {
4-
* this.val = val;
5-
* this.next = null;
6-
* }
2+
* https://leetcode.com/problems/linked-list-cycle/
3+
* Time O(N) | Space O(N)
4+
* @param {ListNode} head
5+
* @return {boolean}
76
*/
7+
var hasCycle = function(head, seen = new Set()) {
8+
while (head) {/* Time O(N) */
9+
if (seen.has(head)) return true;
10+
11+
seen.add(head);/* Space O(N) */
12+
head = head.next;
13+
}
14+
15+
return false;
16+
}
817

918
/**
19+
* https://leetcode.com/problems/linked-list-cycle/
20+
* Time O(N) | Space O(1)
1021
* @param {ListNode} head
1122
* @return {boolean}
1223
*/
13-
var hasCycle = function (head) {
14-
let set = new Set();
15-
while (head) {
16-
if (set.has(head)) {
17-
return true;
18-
} else {
19-
set.add(head);
20-
head = head.next;
21-
}
24+
var hasCycle = function(head) {
25+
let [ slow, fast ] = [ head, head];
26+
27+
while (fast && fast.next) {/* Time O(N) */
28+
slow = slow.next;
29+
fast = fast.next.next;
30+
31+
const hasCycle = slow === fast;
32+
if (hasCycle) return true;
2233
}
2334

2435
return false;
25-
};
36+
};

Diff for: javascript/143-Reorder-List.js

+32-30
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,51 @@
11
/**
2-
* Definition for singly-linked list.
3-
* function ListNode(val, next) {
4-
* this.val = (val===undefined ? 0 : val)
5-
* this.next = (next===undefined ? null : next)
6-
* }
7-
*/
8-
/**
2+
* https://leetcode.com/problems/reorder-list/
3+
* Time O(N) | Space O(1)
94
* @param {ListNode} head
105
* @return {void} Do not return anything, modify head in-place instead.
116
*/
12-
var reorderList = function (head) {
13-
if (!head) {
14-
return;
15-
}
7+
var reorderList = function(head) {
8+
const mid = getMid(head); /* Time O(N) */
9+
const reveredFromMid = reverse(mid);/* Time O(N) */
1610

17-
let slow = head;
18-
let fast = head;
11+
reorder(head, reveredFromMid); /* Time O(N) */
12+
};
13+
14+
const getMid = (head) => {
15+
let [ slow, fast ] = [ head, head ];
1916

20-
// finding the middle of the linked list using 2 pters
21-
while (fast && fast.next) {
17+
while (fast && fast.next) { /* Time O(N) */
2218
slow = slow.next;
2319
fast = fast.next.next;
2420
}
2521

26-
// reverse the second part of the list starting at slow
27-
let prev = null;
28-
let curr = slow;
29-
while (curr) {
30-
let next = curr.next;
22+
return slow;
23+
}
24+
25+
const reverse = (head) => {
26+
let [ prev, curr, next ] = [ null, head, null ];
27+
28+
while (curr) { /* Time O(N) */
29+
next = curr.next;
3130
curr.next = prev;
31+
3232
prev = curr;
3333
curr = next;
34-
} // here prev is the head
34+
}
3535

36-
// merge two sorted lists (first one starts at head, second at prev)
37-
let first = head;
38-
let second = prev;
36+
return prev;
37+
}
3938

40-
while (second.next) {
41-
temp = first.next;
39+
const reorder = (l1, l2) => {
40+
let [ first, next, second ] = [ l1, null, l2 ];
41+
42+
while (second.next) { /* Time O(N) */
43+
next = first.next;
4244
first.next = second;
43-
first = temp;
45+
first = next;
4446

45-
temp = second.next;
47+
next = second.next;
4648
second.next = first;
47-
second = temp;
49+
second = next;
4850
}
49-
};
51+
}

0 commit comments

Comments
 (0)