From c7db4daae01e59ae4422c43c0dd20b56c991408f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tasha=28=EC=82=B4=EB=AF=B8=29?= <45252527+Lustellz@users.noreply.github.com> Date: Sat, 20 Sep 2025 15:06:05 +0900 Subject: [PATCH] solution on linked-list-cycle --- linked-list-cycle/Lustellz.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 linked-list-cycle/Lustellz.ts diff --git a/linked-list-cycle/Lustellz.ts b/linked-list-cycle/Lustellz.ts new file mode 100644 index 000000000..4c7a7366e --- /dev/null +++ b/linked-list-cycle/Lustellz.ts @@ -0,0 +1,29 @@ +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ + +// Runtime: 49ms +// Memory: 56.94MB + +function hasCycle(head: ListNode | null): boolean { + let fast: ListNode = head; + let slow: ListNode = head; + + while (fast && fast.next) { + fast = fast.next.next; + slow = slow!.next; + + if (fast === slow) { + return true; + } + } + return false; +}