From 81334651a16e963868f727014f5d7883a543cdfa Mon Sep 17 00:00:00 2001 From: TheGeniusOfEternity Date: Sat, 15 Aug 2026 16:53:51 +0300 Subject: [PATCH 1/3] test(neetcode|roadmap|linked-list-cycle): testcases --- .../linked-list-cycle/solution.test.ts | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/neetcode/linked-list/linked-list-cycle/solution.test.ts diff --git a/src/neetcode/linked-list/linked-list-cycle/solution.test.ts b/src/neetcode/linked-list/linked-list-cycle/solution.test.ts new file mode 100644 index 0000000..1ce8939 --- /dev/null +++ b/src/neetcode/linked-list/linked-list-cycle/solution.test.ts @@ -0,0 +1,50 @@ +import { createList, ListNode } from '../common'; +import { hasCycle } from './solution'; + +describe('Linked List Cycle | NeetCode | RoadMap | Testcases', () => { + test('#1 Returns false for an empty list', () => { + expect(hasCycle(null)).toBe(false); + }); + + test('#2 Returns false for a list without a cycle', () => { + const head = createList([1, 2, 3, 4]); + + expect(hasCycle(head)).toBe(false); + }); + + test('#3 Returns false for a single node without a cycle', () => { + const head = new ListNode(1); + + expect(hasCycle(head)).toBe(false); + }); + + test('#4 Detects a cycle that starts in the middle of the list', () => { + const first = new ListNode(3); + const cycleStart = new ListNode(2); + const third = new ListNode(0); + const tail = new ListNode(-4); + + first.next = cycleStart; + cycleStart.next = third; + third.next = tail; + tail.next = cycleStart; + + expect(hasCycle(first)).toBe(true); + }); + + test('#5 Detects a cycle that points back to the head', () => { + const first = new ListNode(1); + const second = new ListNode(2); + first.next = second; + second.next = first; + + expect(hasCycle(first)).toBe(true); + }); + + test('#6 Detects a self-referencing node', () => { + const head = new ListNode(1); + head.next = head; + + expect(hasCycle(head)).toBe(true); + }); +}); From f34acee9b2a1219aaa6fef772767fd8bde235f70 Mon Sep 17 00:00:00 2001 From: TheGeniusOfEternity Date: Sat, 15 Aug 2026 16:54:04 +0300 Subject: [PATCH 2/3] feat(neetcode|roadmap|linked-list-cycle): two pointers solution --- .../linked-list/linked-list-cycle/solution.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/neetcode/linked-list/linked-list-cycle/solution.ts diff --git a/src/neetcode/linked-list/linked-list-cycle/solution.ts b/src/neetcode/linked-list/linked-list-cycle/solution.ts new file mode 100644 index 0000000..9357bc4 --- /dev/null +++ b/src/neetcode/linked-list/linked-list-cycle/solution.ts @@ -0,0 +1,21 @@ +import { ListNode } from '../common'; + +/** + * @param {ListNode} head - the beginning of a linked list + * @return {boolean} - `true` if there is a cycle in the linked list, `false` otherwise. + */ +export const hasCycle = (head: ListNode | null): boolean => { + let slow: ListNode | null = head; + let fast: ListNode | null = head; + + while (fast !== null && fast.next !== null) { + slow = slow?.next ?? null; + fast = fast.next.next; + + if (slow === fast) { + return true; + } + } + + return false; +}; From fa68f7cb71ab7fbfae32f6fe38e13cf6f1887569 Mon Sep 17 00:00:00 2001 From: TheGeniusOfEternity Date: Sat, 15 Aug 2026 16:54:20 +0300 Subject: [PATCH 3/3] feat(neetcode|roadmap|docs): update table of contents --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3d8c2c1..668fa5d 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,7 @@ - Linked List - [Task #34](src/neetcode/linked-list/reversed-linked-list) – _Reversed Linked List_ - _[check the task](https://neetcode.io/problems/reverse-a-linked-list)_ - [Task #35](src/neetcode/linked-list/merge-two-sorted-lists) – _Merge Two Sorted Lists_ - _[check the task](https://neetcode.io/problems/merge-two-sorted-linked-lists)_ + - [Task #36](src/neetcode/linked-list/linked-list-cycle) – _Linked List Cycle_ - _[check the task](https://neetcode.io/problems/linked-list-cycle-detection)_ [BigFrontEnd - JavaScript Coding Questions](https://bigfrontend.dev/problem) - [1. implement curry()](src/bigfrontend/javascript/curry) - _Curry Function_ - [check the task](https://bigfrontend.dev/problem/implement-curry)