Skip to content

Commit fbe87a8

Browse files
authored
Merge pull request larissalages#82 from cheshtaaagarrwal/patch-1
141_has cycle.java
2 parents 72bc2d5 + a9c267a commit fbe87a8

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

leetcode/java/linkedlist/141.java

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

0 commit comments

Comments
 (0)