Linked List Cycle LeetCode solution in JavaScript
1/**2* Definition for singly-linked list.3* function ListNode(val) {4* this.val = val;5* this.next = null;6* }7*/89/**10* @param {ListNode} head11* @return {boolean}12*/13function hasCycle(head) {14let fast = head;15while (fast && fast.next) {16head = head.next;17fast = fast.next.next;18if (head === fast) return true;19}20//head and first pointers value in21//each iteration with head=[3,2,0,-4], pos = 122//1st iteration: 3 323//2nd iteration: 2 024//3rd iteration: 0 225//final iteration: -4 -426return false;27}