Skip to content

TypeScript: Chapter 4 Fast and Slow Pointers #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions typescript/Fast and Slow Pointers/happy_number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function happyNumber(n: number): boolean {
let slow = n, fast = n;
while (true) {
slow = getNextNum(slow);
fast = getNextNum(getNextNum(fast));
if (fast == 1)
return true;
// If the fast and slow pointers meet, a cycle is detected.
// Hence, 'n' is not a happy number.
else if (fast == slow)
return false;
}
}

function getNextNum(x: number): number {
let nextNum = 0;
while (x > 0){
// Extract the last digit of 'x'.
const digit = x % 10;
// Truncate (remove) the last digit from 'x' using floor
// division.
x /= 10;
// Add the square of the extracted digit to the sum.
nextNum += digit ** 2;
}
return nextNum;
}
24 changes: 24 additions & 0 deletions typescript/Fast and Slow Pointers/linked_list_loop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ListNode } from "./ds";

/* Definition of ListNode:
class ListNode {
val: number; next: ListNode | null;
constructor(val: number) {
this.val = val;
this.next = null;
}
}*/

function linkedListLoop(head: ListNode | null): boolean {
let slow: ListNode | null = head;
let fast: ListNode | null = head;
// Check both 'fast' and 'fast.next' to avoid null pointer
// exceptions when we perform 'fast.next' and 'fast.next.next'.
while (fast !== null && fast.next !== null) {
slow = slow!.next;
fast = fast.next.next!;
if (fast == slow)
return true;
}
return false;
}
24 changes: 24 additions & 0 deletions typescript/Fast and Slow Pointers/linked_list_loop_naive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ListNode } from "./ds";

/* Definition of ListNode:
class ListNode {
val: number; next: ListNode | null;
constructor(val: number) {
this.val = val;
this.next = null;
}
}*/


function linkedListLoopNaive(head: ListNode | null): boolean {
const visited: Set<ListNode> = new Set();
let curr: ListNode | null = head;
while (curr !== null) {
// Cycle detected if the current node has already been visited.
if (visited.has(curr))
return true;
visited.add(curr);
curr = curr.next;
}
return false;
}
23 changes: 23 additions & 0 deletions typescript/Fast and Slow Pointers/linked_list_midpoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ListNode } from "./ds";

/* Definition of ListNode:
class ListNode {
val: number; next: ListNode | null;
constructor(val: number) {
this.val = val;
this.next = null;
}
}*/


function linkedListMidpoint(head: ListNode | null): ListNode | null {
let slow: ListNode | null = head;
let fast: ListNode | null = head;
// When the fast pointer reaches the end of the list, the slow
// pointer will be at the midpoint of the linked list.
while (fast !== null && fast.next !== null) {
slow = slow!.next;
fast = fast.next.next!;
}
return slow;
}