forked from chihungyu1116/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path141 Linked List Cycle.js
More file actions
41 lines (35 loc) · 801 Bytes
/
Copy path141 Linked List Cycle.js
File metadata and controls
41 lines (35 loc) · 801 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Leetcode #141
// Language: Javascript
// Problem: https://leetcode.com/problems/linked-list-cycle/
// Author: Chihung Yu
/**
* 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 || head.next === null){
return false;
}
var faster = head.next;
var slower = head;
while(faster && slower){
if(faster.val === slower.val){
return true;
}
faster = faster.next;
if(faster === null){
return false;
} else {
faster = faster.next;
}
slower = slower.next;
}
return false;
};