The attached code is what the entire method would look like all together.
The time complexity would be O(n) or linear time.
xxxxxxxxxx22
function detectCycle(head) { let fast = (slow = head); while (fast && fast.next) { fast = fast.next.next; slow = slow.next; if (fast === slow) { return true; } } return false;}​function Node(val) { this.data = val; this.next = null;}​const parent = new Node(1);const child = new Node(2);parent.next = child;child.next = parent;console.log(detectCycle(parent));OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment


