From a03b76386d013a5cd2364adb633777f1a77e128f Mon Sep 17 00:00:00 2001 From: aikhelis Date: Sun, 2 Feb 2025 13:29:53 +0000 Subject: [PATCH 1/3] ts4: add all solutions chapter 3 --- .../Fast and Slow Pointers/happy_number.ts | 27 +++++++++++++++++++ .../linked_list_loop.ts | 23 ++++++++++++++++ .../linked_list_loop_naive.ts | 22 +++++++++++++++ .../linked_list_midpoint.ts | 22 +++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 typescript/Fast and Slow Pointers/happy_number.ts create mode 100644 typescript/Fast and Slow Pointers/linked_list_loop.ts create mode 100644 typescript/Fast and Slow Pointers/linked_list_loop_naive.ts create mode 100644 typescript/Fast and Slow Pointers/linked_list_midpoint.ts diff --git a/typescript/Fast and Slow Pointers/happy_number.ts b/typescript/Fast and Slow Pointers/happy_number.ts new file mode 100644 index 0000000..64b92cb --- /dev/null +++ b/typescript/Fast and Slow Pointers/happy_number.ts @@ -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; +} \ No newline at end of file diff --git a/typescript/Fast and Slow Pointers/linked_list_loop.ts b/typescript/Fast and Slow Pointers/linked_list_loop.ts new file mode 100644 index 0000000..a50a2d4 --- /dev/null +++ b/typescript/Fast and Slow Pointers/linked_list_loop.ts @@ -0,0 +1,23 @@ +/* Definition of ListNode: +class ListNode { + val: number; next: ListNode | null; + constructor(val: number) { + this.val = val; + this.next = null; + } +} +*/ + +function linkedListLoop(head: ListNode): 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; +} \ No newline at end of file diff --git a/typescript/Fast and Slow Pointers/linked_list_loop_naive.ts b/typescript/Fast and Slow Pointers/linked_list_loop_naive.ts new file mode 100644 index 0000000..667afa5 --- /dev/null +++ b/typescript/Fast and Slow Pointers/linked_list_loop_naive.ts @@ -0,0 +1,22 @@ +/* Definition of ListNode: +class ListNode { + val: number; next: ListNode | null; + constructor(val: number) { + this.val = val; + this.next = null; + } +} +*/ + +function linkedListLoopNaive(head: ListNode): boolean { + const visited: Set = 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; +} \ No newline at end of file diff --git a/typescript/Fast and Slow Pointers/linked_list_midpoint.ts b/typescript/Fast and Slow Pointers/linked_list_midpoint.ts new file mode 100644 index 0000000..0a849ed --- /dev/null +++ b/typescript/Fast and Slow Pointers/linked_list_midpoint.ts @@ -0,0 +1,22 @@ +/* Definition of ListNode: +class ListNode { + val: number; next: ListNode | null; + constructor(val: number) { + this.val = val; + this.next = null; + } +} +*/ + + +function linkedListMidpoint(head: ListNode): 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; +} \ No newline at end of file From 2b54c766c271e279510bbe5a9ccdb2f541a70a3e Mon Sep 17 00:00:00 2001 From: aikhelis Date: Sun, 2 Feb 2025 14:12:41 +0000 Subject: [PATCH 2/3] ts-4: eof --- typescript/Fast and Slow Pointers/happy_number.ts | 2 +- typescript/Fast and Slow Pointers/linked_list_loop.ts | 2 +- typescript/Fast and Slow Pointers/linked_list_loop_naive.ts | 2 +- typescript/Fast and Slow Pointers/linked_list_midpoint.ts | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/typescript/Fast and Slow Pointers/happy_number.ts b/typescript/Fast and Slow Pointers/happy_number.ts index 64b92cb..b17b510 100644 --- a/typescript/Fast and Slow Pointers/happy_number.ts +++ b/typescript/Fast and Slow Pointers/happy_number.ts @@ -24,4 +24,4 @@ function getNextNum(x: number): number { nextNum += digit ** 2; } return nextNum; -} \ No newline at end of file +} diff --git a/typescript/Fast and Slow Pointers/linked_list_loop.ts b/typescript/Fast and Slow Pointers/linked_list_loop.ts index a50a2d4..403b0a2 100644 --- a/typescript/Fast and Slow Pointers/linked_list_loop.ts +++ b/typescript/Fast and Slow Pointers/linked_list_loop.ts @@ -20,4 +20,4 @@ function linkedListLoop(head: ListNode): boolean { return true; } return false; -} \ No newline at end of file +} diff --git a/typescript/Fast and Slow Pointers/linked_list_loop_naive.ts b/typescript/Fast and Slow Pointers/linked_list_loop_naive.ts index 667afa5..104d665 100644 --- a/typescript/Fast and Slow Pointers/linked_list_loop_naive.ts +++ b/typescript/Fast and Slow Pointers/linked_list_loop_naive.ts @@ -19,4 +19,4 @@ function linkedListLoopNaive(head: ListNode): boolean { curr = curr.next; } return false; -} \ No newline at end of file +} diff --git a/typescript/Fast and Slow Pointers/linked_list_midpoint.ts b/typescript/Fast and Slow Pointers/linked_list_midpoint.ts index 0a849ed..5293b2f 100644 --- a/typescript/Fast and Slow Pointers/linked_list_midpoint.ts +++ b/typescript/Fast and Slow Pointers/linked_list_midpoint.ts @@ -19,4 +19,4 @@ function linkedListMidpoint(head: ListNode): ListNode | null { fast = fast.next.next!; } return slow; -} \ No newline at end of file +} From 4e542dce54dd665a53fc34874a45e65945c94c37 Mon Sep 17 00:00:00 2001 From: aikhelis Date: Mon, 3 Feb 2025 13:30:09 +0000 Subject: [PATCH 3/3] ts-4: address review comments (import, ditch extra type def) --- typescript/Fast and Slow Pointers/linked_list_loop.ts | 9 +++++---- .../Fast and Slow Pointers/linked_list_loop_naive.ts | 8 +++++--- .../Fast and Slow Pointers/linked_list_midpoint.ts | 9 +++++---- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/typescript/Fast and Slow Pointers/linked_list_loop.ts b/typescript/Fast and Slow Pointers/linked_list_loop.ts index 403b0a2..249a8b1 100644 --- a/typescript/Fast and Slow Pointers/linked_list_loop.ts +++ b/typescript/Fast and Slow Pointers/linked_list_loop.ts @@ -1,14 +1,15 @@ -/* Definition of ListNode: +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): boolean { +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 diff --git a/typescript/Fast and Slow Pointers/linked_list_loop_naive.ts b/typescript/Fast and Slow Pointers/linked_list_loop_naive.ts index 104d665..154167c 100644 --- a/typescript/Fast and Slow Pointers/linked_list_loop_naive.ts +++ b/typescript/Fast and Slow Pointers/linked_list_loop_naive.ts @@ -1,3 +1,5 @@ +import { ListNode } from "./ds"; + /* Definition of ListNode: class ListNode { val: number; next: ListNode | null; @@ -5,10 +7,10 @@ class ListNode { this.val = val; this.next = null; } -} -*/ +}*/ + -function linkedListLoopNaive(head: ListNode): boolean { +function linkedListLoopNaive(head: ListNode | null): boolean { const visited: Set = new Set(); let curr: ListNode | null = head; while (curr !== null) { diff --git a/typescript/Fast and Slow Pointers/linked_list_midpoint.ts b/typescript/Fast and Slow Pointers/linked_list_midpoint.ts index 5293b2f..bf54e69 100644 --- a/typescript/Fast and Slow Pointers/linked_list_midpoint.ts +++ b/typescript/Fast and Slow Pointers/linked_list_midpoint.ts @@ -1,15 +1,16 @@ -/* Definition of ListNode: +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): ListNode | 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