Skip to content

Commit e704e3e

Browse files
committed
Added linked list cycle solution
1 parent 4468d02 commit e704e3e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

linked-list-cycle/nhistory.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
var hasCycle = function (head) {
14+
// Make fast pointer
15+
// Fast pointer will move two steps further inside of list
16+
let fast = head;
17+
// Iterate until fast pointer and head is equal
18+
while (fast && fast.next) {
19+
head = head.next;
20+
fast = fast.next.next;
21+
if (head == fast) return true;
22+
}
23+
return false;
24+
};

0 commit comments

Comments
 (0)