This is a circular linked list program but i am not able to fix the errors

public class circularLL {
private class Node{
int data;
Node next;
}
private Node head=null;
private Node tail=null;
private int size;

public void display() {
Node current = this.head;
while(current!=head) {
System.out.print(current.data+" ");
current=current.next;
}

}
public void addLast(int item) {
//create a new node
Node nn =new Node();
nn.data=item;
nn.next=null;
if(this.size==0) {
this.head=nn;
this .tail=nn;
nn.next=head;
this.size++;
}else {
this.tail.next=nn;
this.tail=nn;
this.tail.next=head;
this.size++;
}
}
public void addFirst(int item) {
//create a new node
Node nn= new Node();
nn.data=item;
nn.next=null;
//attach
if(this.size>=1) {
nn.next=head;

	}
	//summary object
	 if(this.size==0) {
		this.head=nn;
		this.tail=nn;
		nn.next=head;
		this.size++;
	}else {
		this.head=nn;
		this.size++;
	}
}

public int getFirst() throws Exception {
	if(this.size==0) {
		throw new Exception("LL is empty");
	}
	return this.head.data;
}

public int getLast() throws Exception {
	if(this.size==0) {
		throw new Exception("LL is Empty");
	}
	return this.tail.data;
}

public int getAt(int idx) throws Exception {
	if(this.size==0) {
		throw new Exception("LL is empty");
		
	}
	if(idx<0||idx>=this.size) {
		throw new Exception("invalid index");
	}
	Node temp= this.head;
	for(int i=1;i<=idx;i++) {
		temp=temp.next;
	}
	return temp.data;
}

private Node getNodeAt(int idx) throws Exception{
	if(this.size==0) {
		throw new Exception("LL is empty");
		
	}
	if(idx<0||idx>=this.size) {
		throw new Exception("invalid index");
	}
	Node temp= this.head;
	for(int i=1;i<=idx;i++) {
		temp=temp.next;
	}
	return temp;
}
public void addAt(int item,int idx) throws Exception{
	if(idx<0||idx>this.size) {
		throw new Exception("invalid index");
	}
	if(idx==0) {
		addFirst(item);
	}
	else if(idx==this.size) {
		addLast(item);
	}else {
		//create a new node 
		Node nn = new Node();
		nn.data=item;
		nn.next=null;
		//attach
		Node nm1=getNodeAt(idx-1);
		Node np1=getNodeAt(idx);
		nm1.next=nn;
		nn.next=np1;
		this.size++;
	}
}
public int removeFirst() throws Exception{
	if(this.size==0) {
		throw new Exception("LL is empty");
		
	}
	int rv= this.head.data;
	//summary
	if(this.size==1) {
		this.head=null;
		this.tail=null;
		this.size=0;
	}else {
		this.head=this.head.next;
	}
	return rv;
}

public int removeLast() throws Exception {
	if(this.size==0) {
		throw new Exception("LL is empty");
	}
	int rv=this.tail.data;
	if(this.size==1) {
		this.head=null;
		this.tail=null;
		this.size=0;
		
	}else {
		Node sizem2 = getNodeAt(this.size-2);
		this.tail=sizem2;
		this.tail.next=null;
		this.size--;
	}
	return rv;
}
public int removeAt(int idx) throws Exception{
	if(this.size==0) {
		throw new Exception("LL is Empty");
	}
	if(idx<0||idx>=this.size) {
		throw new Exception("Invalid index");
		
	}
	if(idx==0) {
		return removeFirst();
	}else if(idx==this.size-1) {
		return removeLast();
	}else {
		Node nm1 = getNodeAt(idx-1);
		Node n = nm1.next;
		Node np1=n.next;
		nm1.next=np1;
		this.size--;
		return n.data;
	}
}

public static void main(String[] args) throws Exception {
	// TODO Auto-generated method stub

circularLL list = new circularLL();
list.addFirst(2);
list.addFirst(3);
list.addLast(4);
list.addLast(5);
list.addLast(6);
list.addLast(7);
list.addFirst(2);
//list.removeLast();
//list.removeAt(2);
//System.out.println(list.getAt(2));
list.display();

}

}

@Shivam_balwani,

Your code is not correct. You are given a circular linked list and you have to detect the cycle in it and remove the cycle.

Say If INPUT IS 1 2 3 4 5 2 3 -1

Form a linked list 1->2->3->4>5 then you see that 2 has already been visited , so connect the next of 5 to 2 (which has already been created). This way you have created the circular linked list . After that just detect the cycle, remove the cycle, print the new linked list.

Maintain a map or a visited array…whenever you see that a node is already visited, connect next of previous node to that node

On how to remove the circular loop:

We will use Floyd Cycle Detection algorithm to detect and remove the loop. In the Floyd’s algo, the slow and fast pointers meet at a loop node. We can use this loop node to remove cycle. There are following two different ways of removing loop when Floyd’s algorithm is used for Loop detection.

Method 1 (Check one by one):

  1. We know that Floyd’s Cycle detection algorithm terminates when fast and slow pointers meet at a common point. We also know that this common point is one of the loop nodes (2 or 3 or 4 or 5 in the above diagram).
  2. We store the address of this in a pointer variable say ptr2.
  3. Then we start from the head of the Linked List and check for nodes one by one if they are reachable from ptr2.
  4. When we find a node that is reachable, we know that this node is the starting node of the loop in Linked List and we can get pointer to the previous of this node.

Method 2 (Better Solution) :
This method is also dependent on Floyd’s Cycle detection algorithm.

  1. Detect Loop using Floyd’s Cycle detection algo and get the pointer to a loop node.
  2. Count the number of nodes in loop. Let the count be k.
  3. Fix one pointer to the head and another to kth node from head.
  4. Move both pointers at the same pace, they will meet at loop starting node.
  5. Get pointer to the last node of loop and make next of it as NULL.

Also, please share your code through: https://ide.codingblocks.com/
Steps to share your code:

  1. Select language. copy your code.
  2. press ctrl+s and click on save
  3. share the url with me.