Skip to content

Instantly share code, notes, and snippets.

@lienista
Last active December 5, 2021 04:39
Show Gist options
  • Select an option

  • Save lienista/3fb7326d587973cf068340ad6e2b5cf5 to your computer and use it in GitHub Desktop.

Select an option

Save lienista/3fb7326d587973cf068340ad6e2b5cf5 to your computer and use it in GitHub Desktop.
(Algorithms in Javascript) Leetcode 141. Linked List Cycle - Given a linked list, determine if it has a cycle in it.
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {boolean}
*/
const hasCycle = (head) => {
let slow, fast;
slow = fast = head;
while(fast && fast.next){
slow = slow.next;
fast = fast.next.next;
if(slow === fast){
return true;
}
}
return false;
};
@xinxin510
Copy link

The values of variable slow and variable fast are linked list. How can we use triple equal to compare them directly? Does slow === fast return false?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment