Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Program to Detect and Remove Loop in a Linked List in Java?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 988
    Comment on it
    // Program to detect and remove loop in linked list in java
     
    class LinkedList     //create a node in LinkedList 
    {
     
        static Node head;
     
        static class Node   //create a node in LinkedList 
        {
            int data;
            Node next;
     
            Node(int d) 
           {
                data = d;
                next = null;
            }
        }
     
        void detectAndRemoveLoop(Node node)  //Function detecting and removing Loop in LinkedList
         {
            Node slow = node;
            Node fast = node.next;
     
            while (fast != null && fast.next != null) //Searching for loop
               {
                if (slow == fast)  //if loop exist break 
                {
                    break;
                }
                slow = slow.next;
                fast = fast.next.next;
            }
     
            if (slow == fast)  //if condition executed if loop exist 
            {
                slow = node;
                while (slow != fast.next) 
                {
                    slow = slow.next;
                    fast = fast.next;
                }
     
                fast.next = null; // removing loop 
     
            }
        }
     
        void printList(Node node)   //Function to print LinkedList 
        {
            while (node != null) 
           {
                System.out.print(node.data + " ");
                node = node.next;
            }
        }
     
        public static void main(String[] args) 
        {
            LinkedList list = new LinkedList();
            list.head = new Node(50);
            list.head.next = new Node(20);
            list.head.next.next = new Node(15);
            list.head.next.next.next = new Node(4);
            list.head.next.next.next.next = new Node(10);
     
            // Creating a loop for testing 
            head.next.next.next.next.next = head.next.next;
            list.detectAndRemoveLoop(head);
            System.out.println("Linked List after removing loop : ");
            list.printList(head);
        }
    }
     

    Output:

    Linked List after removing loop 
    50 20 15 4 10 

    Explanation:

    In java there is no concept of pointers. A class LinkedList is created within which function for creating a node, detecting loop in LinkedList and printing of LinkedList is defined. Two nodes slow and fast are used to detect loop. Head value is passed to function detectandRemoveLoop in which slow node contain value of head while fast node contain value of head.next. The slow node is incremented by one i.e slow.next and fast pointer by two i.e fast.next.next inside while loop and  if loop exist the slow node equals fast node, the while loop breaks which checks detection of loop. If loop does not exist then in while loop either fast is NULL or fast.next is NULL and while loop ends. For removing loop fast.next should be equal to NULL.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: