This is my code .
import java.util.*;
public class Main {
public static void main(String args[]) {
LinkedList ll = new LinkedList() ;
ll.removeLoop() ;
ll.display() ;
}
}
class LinkedList{
private class Node{
int data ;
Node next ;
Node(int data){
this.data = data ;
}
}
Node head ;
Node curr ;
int size ;
LinkedList(){
buildList() ;
}
private void buildList(){
Scanner scan = new Scanner(System.in) ;
HashMap<Integer,Node> map = new HashMap<>() ;
int data = scan.nextInt() ;
Node n1 = new Node(data) ;
head = n1 ;
curr = n1 ;
size ++ ;
map.put(head.data,head) ;
int x = scan.nextInt() ;
while(x != -1){
if(!map.containsKey(x)){
Node nn = new Node(x) ;
curr.next = nn ;
curr = nn ;
map.put(curr.data,curr) ;
size ++ ;
}
else{
Node addr = map.get(x) ;
curr.next = addr ;
curr = curr.next ;
}
x = scan.nextInt() ;
}
}
public void removeLoop(){
Node slow = head ;
Node fast = head ;
while(true){
slow = slow.next ;
fast = fast.next.next ;
if(slow == fast){
break ;
}
}
slow = head ;
while(slow.next != fast.next){
slow = slow.next ;
fast = fast.next ;
}
fast.next = null ;
}
public void display(){
Node temp = head ;
while(temp != null){
System.out.print(temp.data+" ") ;
temp = temp.next ;
}
}
}
But some test cases are failing . Can you tell me where did I go wrong in my logic .