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
*/
8

9
/**
10
* @param {ListNode} head
11
* @return {boolean}
12
*/
13
function hasCycle(head) {
14
let fast = head;
15
while (fast && fast.next) {
16
head = head.next;
17
fast = fast.next.next;
18
if (head === fast) return true;
19
}
20
//head and first pointers value in
21
//each iteration with head=[3,2,0,-4], pos = 1
22
//1st iteration: 3 3
23
//2nd iteration: 2 0
24
//3rd iteration: 0 2
25
//final iteration: -4 -4
26
return false;
27
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0