Skip to main content

Leetcode - 141. Linked List Cycle

· One min read

https://leetcode.com/problems/linked-list-cycle/

Solution 1

/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/

/**
* @param {ListNode} head
* @return {boolean}
*/
var hasCycle = function (head) {
if (head === null) {
return false;
}

let curr = head;
let next = head.next;

while (next && next.next) {
curr = curr.next;
next = next.next.next;

if (curr === next) {
return true;
}
}

return false;
};