Skip to content

Commit

Permalink
Create Linked List Cycle II.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayu-99 authored Jan 19, 2022
1 parent 9258590 commit 7d0d584
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Leetcode Challenge/January/Java/Linked List Cycle II.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public ListNode detectCycle(ListNode head) {
ListNode slow = head, fast = head;
while(fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (slow == fast) {
while (head != slow) {
head = head.next;
slow = slow.next;
}
return slow;
}
}
return null;
}

0 comments on commit 7d0d584

Please sign in to comment.