Advertisement
unknown_0711

Untitled

Jan 8th, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. class Solution
  2. {
  3. public static void removeLoop(Node head){
  4.  
  5. Node fast = head.next;
  6. Node slow = head;
  7. while( fast!=slow )
  8. {
  9. if( fast==null || fast.next==null )
  10. return;
  11.  
  12. fast = fast.next.next;
  13. slow = slow.next;
  14. }
  15. int size = 1;
  16. fast = fast.next;
  17. while( fast!=slow )
  18. {
  19. fast = fast.next;
  20. size+=1;
  21. }
  22. slow = head;
  23. fast = head;
  24.  
  25. for(int i=0; i<size-1; i++)
  26. fast = fast.next;
  27.  
  28. while( fast.next != slow )
  29. {
  30. fast = fast.next;
  31. slow = slow.next;
  32. }
  33. fast.next = null;
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement