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; +}