- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlinkedListCycle.java
More file actions
Latest commit
29 lines (27 loc) · 653 Bytes
/
Copy pathlinkedListCycle.java
File metadata and controls
29 lines (27 loc) · 653 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
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
publicclassSolution {
publicbooleanhasCycle(ListNodehead) {
publicbooleanhasCycle(ListNodehead) {
ListNodefast_iter = head;
ListNodeslow_iter = head;
while (fast_iter!=null && fast_iter.next!=null) {
slow_iter = slow_iter.next;
fast_iter = fast_iter.next.next;
if (fast_iter==slow_iter) {
returntrue;
}
}
returnfalse;
}
}
}