Last active
December 5, 2021 04:39
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?