-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathfastAndSlowPointers.ts
86 lines (79 loc) · 2.58 KB
/
fastAndSlowPointers.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
class ListNode {
val: number;
next: ListNode | null = null;
constructor(x: number) {
this.val = x;
}
}
class FastAndSlowPointers {
// LeetCode 141 - Linked List Cycle (HashSet Approach)
hasCycleHashSetApproach(head: ListNode | null): boolean {
const visited = new Set<ListNode>();
let current = head;
while (current) {
if (visited.has(current)) {
return true;
}
visited.add(current);
current = current.next;
}
return false;
}
// LeetCode 141 - Linked List Cycle (Fast and Slow Pointer Approach)
hasCycleFastAndSlowPointersApproach(head: ListNode | null): boolean {
if (!head || !head.next) return false;
let slow: ListNode | null = head;
let fast: ListNode | null = head;
while (fast && fast.next) {
slow = slow!.next;
fast = fast.next.next;
if (slow === fast) return true;
}
return false;
}
// LeetCode 876 - Middle of the Linked List (Counting Approach)
middleNodeCountingApproach(head: ListNode | null): ListNode | null {
let count = 0;
let current = head;
while (current) {
count++;
current = current.next;
}
current = head;
for (let i = 0; i < Math.floor(count / 2); i++) {
current = current!.next;
}
return current;
}
// LeetCode 876 - Middle of the Linked List (Fast and Slow Pointer Approach)
middleNodeFastAndSlowPointerApproach(head: ListNode | null): ListNode | null {
let slow = head, fast = head;
while (fast && fast.next) {
slow = slow!.next;
fast = fast.next.next;
}
return slow;
}
// LeetCode 202 - Happy Number (HashSet Approach)
getSumOfSquares(n: number): number {
return String(n).split('').reduce((sum, digit) => sum + Number(digit) ** 2, 0);
}
isHappyHashSetApproach(n: number): boolean {
const seen = new Set<number>();
while (n !== 1 && !seen.has(n)) {
seen.add(n);
n = this.getSumOfSquares(n);
}
return n === 1;
}
// LeetCode 202 - Happy Number (Fast and Slow Pointer Approach)
isHappyFastAndSlowPointersApproach(n: number): boolean {
let slow = n;
let fast = this.getSumOfSquares(n);
while (fast !== 1 && slow !== fast) {
slow = this.getSumOfSquares(slow);
fast = this.getSumOfSquares(this.getSumOfSquares(fast));
}
return fast === 1;
}
}