Skip to content

Commit 88400ee

Browse files
committed
LinkedList Cycle:AC
1 parent 938f1b2 commit 88400ee

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package leetcode.common;
2+
3+
/**
4+
* @author Kyle
5+
* @create 2018/8/23 1:10
6+
*/
7+
public class ListNode {
8+
public int val;
9+
public ListNode next;
10+
public ListNode(int x) {
11+
val = x;
12+
next = null;
13+
}
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package leetcode.easy;
2+
3+
import leetcode.common.ListNode;
4+
5+
/**
6+
* @author Kyle
7+
* @create 2018/8/23 1:08
8+
*/
9+
public class LinkedListCycle {
10+
public boolean hasCycle(ListNode head) {
11+
if(head == null) {
12+
return false;
13+
}
14+
15+
ListNode fast = head;
16+
ListNode slow = head;
17+
18+
while (fast.next != null && fast.next.next != null) {
19+
slow = slow.next;
20+
fast = fast.next.next;
21+
if(fast != null && slow == fast) {
22+
return true;
23+
}
24+
}
25+
26+
return false;
27+
}
28+
}

0 commit comments

Comments
 (0)