Advertisement
Guest User

Grokking 222

a guest
Jun 9th, 2022
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. public ListNode detectCycle(ListNode head) {
  2. ListNode fast = head;
  3. ListNode slow = head;
  4. ListNode temp = head;
  5. while(fast != null && fast.next != null) {
  6. slow = slow.next;
  7. fast = fast.next.next;
  8.  
  9. if (fast == slow) {
  10. while(temp != slow) {
  11. temp = temp.next;
  12. slow = slow.next;
  13. }
  14. return temp;
  15. }
  16. }
  17.  
  18. return null;
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement