Advertisement
enigmjoe

Big O week 3 day 6 p1

Sep 2nd, 2023
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. public static Node firstNode(Node head) {
  2. Node slow = head, fast = head;
  3. // 1->2->3->4
  4. while(fast.next != null && fast.next.next != null){
  5. slow = slow.next;
  6. fast = fast.next.next;
  7. if(slow == fast){
  8. break;
  9. }
  10. }
  11. if(fast.next == null || fast.next.next == null){
  12. return null;
  13. }
  14. Node slow2 = head;
  15. while(slow.next != null){
  16. if(slow == slow2) return slow;
  17. slow = slow.next;
  18. slow2 = slow2.next;
  19. }
  20. return null;
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement