Skip to content

Commit 4661f40

Browse files
authored
Merge pull request #66 from aikhelis/typescript_solutions_fast_and_slow_pointers_clean
TypeScript: Chapter 4 Fast and Slow Pointers
2 parents 4d249f1 + 4e542dc commit 4661f40

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function happyNumber(n: number): boolean {
2+
let slow = n, fast = n;
3+
while (true) {
4+
slow = getNextNum(slow);
5+
fast = getNextNum(getNextNum(fast));
6+
if (fast == 1)
7+
return true;
8+
// If the fast and slow pointers meet, a cycle is detected.
9+
// Hence, 'n' is not a happy number.
10+
else if (fast == slow)
11+
return false;
12+
}
13+
}
14+
15+
function getNextNum(x: number): number {
16+
let nextNum = 0;
17+
while (x > 0){
18+
// Extract the last digit of 'x'.
19+
const digit = x % 10;
20+
// Truncate (remove) the last digit from 'x' using floor
21+
// division.
22+
x /= 10;
23+
// Add the square of the extracted digit to the sum.
24+
nextNum += digit ** 2;
25+
}
26+
return nextNum;
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { ListNode } from "./ds";
2+
3+
/* Definition of ListNode:
4+
class ListNode {
5+
val: number; next: ListNode | null;
6+
constructor(val: number) {
7+
this.val = val;
8+
this.next = null;
9+
}
10+
}*/
11+
12+
function linkedListLoop(head: ListNode | null): boolean {
13+
let slow: ListNode | null = head;
14+
let fast: ListNode | null = head;
15+
// Check both 'fast' and 'fast.next' to avoid null pointer
16+
// exceptions when we perform 'fast.next' and 'fast.next.next'.
17+
while (fast !== null && fast.next !== null) {
18+
slow = slow!.next;
19+
fast = fast.next.next!;
20+
if (fast == slow)
21+
return true;
22+
}
23+
return false;
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { ListNode } from "./ds";
2+
3+
/* Definition of ListNode:
4+
class ListNode {
5+
val: number; next: ListNode | null;
6+
constructor(val: number) {
7+
this.val = val;
8+
this.next = null;
9+
}
10+
}*/
11+
12+
13+
function linkedListLoopNaive(head: ListNode | null): boolean {
14+
const visited: Set<ListNode> = new Set();
15+
let curr: ListNode | null = head;
16+
while (curr !== null) {
17+
// Cycle detected if the current node has already been visited.
18+
if (visited.has(curr))
19+
return true;
20+
visited.add(curr);
21+
curr = curr.next;
22+
}
23+
return false;
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ListNode } from "./ds";
2+
3+
/* Definition of ListNode:
4+
class ListNode {
5+
val: number; next: ListNode | null;
6+
constructor(val: number) {
7+
this.val = val;
8+
this.next = null;
9+
}
10+
}*/
11+
12+
13+
function linkedListMidpoint(head: ListNode | null): ListNode | null {
14+
let slow: ListNode | null = head;
15+
let fast: ListNode | null = head;
16+
// When the fast pointer reaches the end of the list, the slow
17+
// pointer will be at the midpoint of the linked list.
18+
while (fast !== null && fast.next !== null) {
19+
slow = slow!.next;
20+
fast = fast.next.next!;
21+
}
22+
return slow;
23+
}

0 commit comments

Comments
 (0)