Circular linked list(what does this error means and what is the solution of this)

import java.util.*;
class LinkedList {
private class Node{
int data;
Node next;
}
private Node head;
private Node tail;
private int size;
public void addLast(int data){
Node nn = new Node();
nn.data = data;
nn.next = null;
if(size>0){
this.tail.next = nn;
}
if(size == 0){
this.head = nn;
this.tail = nn;
this.size=1;
}
else {
this.tail = nn;
this.size++;
}
}
public int getFirst() throws Exception{
if(size == 0 ){
throw new Exception(“List is Empty”);
}
else
return this.head.data;
}
int detectAndRemoveLoop(Node node)
{
Node slow = node, fast = node;
while (slow != null && fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;

        // If slow and fast meet at same point then loop is present 
        if (slow == fast) { 
            removeLoop(slow, node); 
            return 1; 
        } 
    } 
    return 0; 
} 

// Function to remove loop 
void removeLoop(Node loop, Node curr) 
{ 
    Node ptr1 = null, ptr2 = null; 

    /* Set a pointer to the beging of the Linked List and 
     move it one by one to find the first node which is 
     part of the Linked List */
    ptr1 = curr; 
    while (1 == 1) { 

        /* Now start a pointer from loop_node and check if it ever 
         reaches ptr2 */
        ptr2 = loop; 
        while (ptr2.next != loop && ptr2.next != ptr1) { 
            ptr2 = ptr2.next; 
        } 

        /* If ptr2 reahced ptr1 then there is a loop. So break the 
         loop */
        if (ptr2.next == ptr1) { 
            break; 
        } 

        /* If ptr2 did't reach ptr1 then try the next node after ptr1 */
        ptr1 = ptr1.next; 
    } 

    /* After the end of loop ptr2 is the last node of the loop. So 
     make next of ptr2 as NULL */
    ptr2.next = null; 
} 

// Function to print the linked list 
void printList(Node node) 
{ 
    while (node != null) { 
        System.out.print(node.data + " "); 
        node = node.next; 
    } 
} 
    public static void main(String args[]) {
		Scanner sc= new Scanner(System.in);
		LinkedList list = new LinkedList();
		while(sc.nextInt()!=-1){
			list.addLast(sc.nextInt());
		}
		list.head = getFirst();
		head.next.next.next.next.next = head.next.next; 
		list.printList(head);
		detectAndRemoveLoop(head);
}

}

while(sc.nextInt()!=-1){
list.addLast(sc.nextInt());
}

the way u r foming the cycle is wrong
pls maintain map - val vs node to form the cycle

to detect a loop: this is a very standard prblm of linkedlist
and this approach is very useful in many prblms
so pls try to find the efficient approach to detect loop

please provide me the coorections and correncted code of my program

ur approach is right
but this is not the efiicient approach to solve this prblm
if i just write the code u would not able uderstand that

so just try to figure out the approach
so search you will easily get that approach
then
if u find any difficulty or mistake in ur code
then i’m ready to help u

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

(please tell me the corrected code )

(please tell me the corrected code ) . package com.codechef.javaapp; import java.util.; class LinkedList { private Node head; public void detectAndRemoveLoop() { this.detectAndRemoveLoop(this.head); } class Node { int data; Node next; Node() { this.data = data; this.next = null; } } private Node tail; private int size; // Function that detects loop in the list private int detectAndRemoveLoop(Node node) { Node slow = node, fast = node; while (slow != null && fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; // If slow and fast meet at same point then loop is present if (slow == fast) { removeLoop(slow, node); this.printList(this.head); } } return 0; } // Function to remove loop void removeLoop(Node loop, Node curr) { Node ptr1 = null, ptr2 = null; / Set a pointer to the beging of the Linked List and move it one by one to find the first node which is part of the Linked List / ptr1 = curr; while (1 == 1) { / Now start a pointer from loop_node and check if it ever reaches ptr2 / ptr2 = loop; while (ptr2.next != loop && ptr2.next != ptr1) { ptr2 = ptr2.next; } / If ptr2 reahced ptr1 then there is a loop. So break the loop / if (ptr2.next == ptr1) { break; } / If ptr2 did’t reach ptr1 then try the next node after ptr1 / ptr1 = ptr1.next; } / After the end of loop ptr2 is the last node of the loop. So make next of ptr2 as NULL */ ptr2.next = null; } // Function to print the linked list void printList(Node node) { while (node != null) { System.out.print(node.data + " "); node = node.next; } } void addLast(int data) { Node nn = new Node(); nn.data = data; nn.next = null; if(size>0){ this.tail.next = nn; } if(size == 0){ this.head = nn; this.tail = nn; this.size=1; } else { this.tail = nn; this.size++; } } } class main{ public static void main(String[] args) { LinkedList list = new LinkedList(); Scanner sc = new Scanner(System.in); while (true){ int n1 = sc.nextInt(); if(n1 == -1){ break; } else { list.addLast(sc.nextInt()); } } list.detectAndRemoveLoop(); } }

can u pls send me the code ide link

i dont know how to send ide

@8006366388
just open coding blocks online ide paste your code there and then save the code and click on share button you will get a shareable link paste the link here

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.