Skip to content

Commit 333536e

Browse files
Create 141. Linked List Cycle.java
1 parent ed259ed commit 333536e

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

java/141. Linked List Cycle.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//Fast and slow pointer
2+
3+
public class Solution {
4+
public boolean hasCycle(ListNode head) {
5+
ListNode fast = head;
6+
ListNode slow = head;
7+
while (fast!=null && fast.next!=null) {
8+
fast = fast.next.next;
9+
slow = slow.next;
10+
if (fast == slow) return true;
11+
}
12+
return false;
13+
}
14+
}

0 commit comments

Comments
 (0)