Skip to content

Commit

Permalink
22
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangshanmeta committed Jan 23, 2025
1 parent 9d2cabf commit 86ffdf6
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/LCR022.c32eOV.1000258/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
if(head == null){
return null;
}
ListNode slow = head;
ListNode 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 86ffdf6

Please sign in to comment.