Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int findLoop(Node n)
- {
- Node slow = head,fast = head;
- while(slow != null && fast != null && fast.next != null)
- {
- slow = slow.next;
- fast = fast.next.next;
- if(slow == fast)
- {
- System.out.println("Found loop");
- return 1;
- }
- }
- return 0;
- }
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- detectLoop dl = new detectLoop();
- System.out.println("Enter the number of elements: ");
- int n = sc.nextInt();
- System.out.println("Enter the values to be inserted: ");
- for (int i = 0; i < n; i++)
- {
- int x = sc.nextInt();
- dl.push(x);
- }
- Node n1 = dl.head;
- while(n1.next != null)
- {
- n1 = n1.next;
- }
- n1.next = dl.head;
- dl.findLoop(n1);
- }
Add Comment
Please, Sign In to add comment