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..b17b510 --- /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; +} 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..249a8b1 --- /dev/null +++ b/typescript/Fast and Slow Pointers/linked_list_loop.ts @@ -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; +} 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..154167c --- /dev/null +++ b/typescript/Fast and Slow Pointers/linked_list_loop_naive.ts @@ -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 = 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; +} 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..bf54e69 --- /dev/null +++ b/typescript/Fast and Slow Pointers/linked_list_midpoint.ts @@ -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; +}