Skip to content

Commit b91ceb1

Browse files
141 has cycle
Added has cycle linked list question
1 parent 8c33fc5 commit b91ceb1

File tree

1 file changed

+16
-0
lines changed
  • leetcode/java/linkedlist

1 file changed

+16
-0
lines changed

leetcode/java/linkedlist/141

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)