|
| 1 | +## Remove loop in Linked List |
| 2 | +Given a linked list of N nodes such that it may contain a loop. |
| 3 | + |
| 4 | +> A loop here means that the last node of the link list is connected to the node at position X. If the link list does not have any loop, X=0. |
| 5 | +
|
| 6 | +Remove the loop from the linked list, if it is present. [🔗Goto](https://practice.geeksforgeeks.org/problems/remove-loop-in-linked-list/1/?page=1&difficulty[]=1&status[]=unsolved&company[]=Amazon&sortBy=submissions#) |
| 7 | + |
| 8 | +<details> |
| 9 | +<summary>Full Code</summary> |
| 10 | + |
| 11 | +``` |
| 12 | +import java.util.*; |
| 13 | +import java.io.*; |
| 14 | +import java.lang.*; |
| 15 | +
|
| 16 | +class Node |
| 17 | +{ |
| 18 | + int data; |
| 19 | + Node next; |
| 20 | +} |
| 21 | +
|
| 22 | +class GFG |
| 23 | +{ |
| 24 | + public static Node newNode(int data){ |
| 25 | + Node temp = new Node(); |
| 26 | + temp.data = data; |
| 27 | + temp.next = null; |
| 28 | + return temp; |
| 29 | + } |
| 30 | + |
| 31 | + public static void makeLoop(Node head, int x){ |
| 32 | + if (x == 0) |
| 33 | + return; |
| 34 | + Node curr = head; |
| 35 | + Node last = head; |
| 36 | +
|
| 37 | + int currentPosition = 1; |
| 38 | + while (currentPosition < x) |
| 39 | + { |
| 40 | + curr = curr.next; |
| 41 | + currentPosition++; |
| 42 | + } |
| 43 | + |
| 44 | + while (last.next != null) |
| 45 | + last = last.next; |
| 46 | + last.next = curr; |
| 47 | + } |
| 48 | + |
| 49 | + public static boolean detectLoop(Node head){ |
| 50 | + Node hare = head.next; |
| 51 | + Node tortoise = head; |
| 52 | + while( hare != tortoise ) |
| 53 | + { |
| 54 | + if(hare==null || hare.next==null) return false; |
| 55 | + hare = hare.next.next; |
| 56 | + tortoise = tortoise.next; |
| 57 | + } |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + public static int length(Node head){ |
| 62 | + int ret=0; |
| 63 | + while(head!=null) |
| 64 | + { |
| 65 | + ret += 1; |
| 66 | + head = head.next; |
| 67 | + } |
| 68 | + return ret; |
| 69 | + } |
| 70 | + |
| 71 | + public static void main (String[] args){ |
| 72 | + Scanner sc = new Scanner(System.in); |
| 73 | + int t = sc.nextInt(); |
| 74 | + |
| 75 | + while(t--> 0) |
| 76 | + { |
| 77 | + int n = sc.nextInt(); |
| 78 | + |
| 79 | + int num = sc.nextInt(); |
| 80 | + Node head = newNode(num); |
| 81 | + Node tail = head; |
| 82 | + |
| 83 | + for(int i=0; i<n-1; i++) |
| 84 | + { |
| 85 | + num = sc.nextInt(); |
| 86 | + tail.next = newNode(num); |
| 87 | + tail = tail.next; |
| 88 | + } |
| 89 | + |
| 90 | + int pos = sc.nextInt(); |
| 91 | + makeLoop(head, pos); |
| 92 | + |
| 93 | + Solution x = new Solution(); |
| 94 | + x.removeLoop(head); |
| 95 | + |
| 96 | + if( detectLoop(head) || length(head)!=n ) |
| 97 | + System.out.println("0"); |
| 98 | + else |
| 99 | + System.out.println("1"); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | +</details> |
| 105 | + |
| 106 | +``` |
| 107 | +class Solution |
| 108 | +{ |
| 109 | + public static void removeLoop(Node head){ |
| 110 | + Node low = head; |
| 111 | + Node high = head; |
| 112 | + while(low!=null && high != null && high.next != null){ |
| 113 | + low = low.next; |
| 114 | + high = high.next.next; |
| 115 | + if(low == high){ |
| 116 | + break; |
| 117 | + } |
| 118 | + } |
| 119 | + if(low == head){ |
| 120 | + while(high.next != low){ |
| 121 | + high = high.next; |
| 122 | + } |
| 123 | + high.next = null; |
| 124 | + }else if(low == high){ |
| 125 | + low = head; |
| 126 | + while(low.next != high.next){ |
| 127 | + low = low.next; |
| 128 | + high = high.next; |
| 129 | + } |
| 130 | + high.next = null; |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | +``` |
0 commit comments