-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleNode.js
More file actions
executable file
·66 lines (56 loc) · 1.61 KB
/
middleNode.js
File metadata and controls
executable file
·66 lines (56 loc) · 1.61 KB
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
// find middle node of a linked list
//Fast & Slow Pointers
//Key insight:
// One pointer moves twice as fast
// example of linked list node
class ListNode {
constructor(value) {
this.value = value;
this.next = null;
}
}
function middleNode(head) {
let slow = head;
let fast = head;
// move fast pointer two steps and slow pointer one step
while (fast !== null && fast.next !== null) {
slow = slow.next;
fast = fast.next.next;
}
return slow; // slow is now at the middle node
}
// time complexity: O(n)
// space complexity: O(1)
// test the function
// helper function to create a linked list from an array
function createLinkedList(arr) {
let head = new ListNode(arr[0]);
let current = head;
for (let i = 1; i < arr.length; i++) {
current.next = new ListNode(arr[i]);
current = current.next;
}
return head;
}
// helper function to print linked list from a given node
function printLinkedListFromNode(node) {
let current = node;
let result = [];
while (current !== null) {
result.push(current.value);
current = current.next;
}
console.log(result.join(" -> "));
}
let list1 = createLinkedList([1, 2, 3, 4, 5]);
let middle1 = middleNode(list1);
printLinkedListFromNode(middle1); // Output: 3 -> 4 -> 5
let list2 = createLinkedList([10, 20, 30, 40]);
let middle2 = middleNode(list2);
printLinkedListFromNode(middle2); // Output: 30 -> 40
let list3 = createLinkedList([7]);
let middle3 = middleNode(list3);
printLinkedListFromNode(middle3); // Output: 7
let list4 = createLinkedList([1, 2]);
let middle4 = middleNode(list4);
printLinkedListFromNode(middle4); // Output: 2