Can you please detect error in the code?
import java.util.*;
public class Main
{
public static void main(String[] args) {
LinkedList ll= new LinkedList();
Scanner sc = new Scanner (System.in);
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
ll.addLast(sc.nextInt());
}
boolean ans=ll.detectRemoveLoop();
if(ans==true)
{
ll.display();
}
else
{
System.out.print("-1");
}
}
}
class LinkedList
{
private class Node
{
int data;
Node next;
}
private Node head;
private Node tail;
private int size;
public void display()
{
Node temp=head;
while(temp!=null)
{
System.out.print(temp.data+" ");
temp=temp.next;
}
}
public void addLast(int item)
{
Node nn= new Node();
nn.data=item;
nn.next=null;
if(size>=1)
{
this.tail.next=nn;
}
if(this.size==0)
{
this.head=nn;
this.tail=nn;
this.size++;
}
else
{
this.tail=nn;
this.size++;
}
}
public boolean detectRemoveLoop()
{
// detect loop
Node slow = head;
Node fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast)
break;
}
if (slow == fast)
{
// loop remove
Node start = head;
Node loop = slow;
while (start.next != loop.next) {
start = start.next;
loop = loop.next;
}
loop.next = null;
return true;
}
else
{
return false;
}
}
}