Guest User

Untitled

a guest
Nov 24th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. public int findLoop(Node n)
  2. {
  3. Node slow = head,fast = head;
  4. while(slow != null && fast != null && fast.next != null)
  5. {
  6. slow = slow.next;
  7. fast = fast.next.next;
  8. if(slow == fast)
  9. {
  10. System.out.println("Found loop");
  11. return 1;
  12. }
  13. }
  14. return 0;
  15. }
  16.  
  17. public static void main(String[] args)
  18. {
  19.  
  20. Scanner sc = new Scanner(System.in);
  21. detectLoop dl = new detectLoop();
  22. System.out.println("Enter the number of elements: ");
  23. int n = sc.nextInt();
  24. System.out.println("Enter the values to be inserted: ");
  25. for (int i = 0; i < n; i++)
  26. {
  27. int x = sc.nextInt();
  28. dl.push(x);
  29. }
  30. Node n1 = dl.head;
  31. while(n1.next != null)
  32. {
  33. n1 = n1.next;
  34. }
  35. n1.next = dl.head;
  36. dl.findLoop(n1);
  37. }
Add Comment
Please, Sign In to add comment