It seems that my linked list is going in infinite loop but idk why?

public void reversePointer() throws Exception {
if (this.size == 0)
throw new Exception(">>> list is empty => reverseData >>>");
Node prev = this.head;
Node curr = prev.next;
int count = 1;
while (curr != null) {

		Node ahead = curr.next;
		curr.next = prev;
		System.out.println(count);
		count++;
		prev = curr;
		curr = ahead;
	}
		Node t = this.head;
	this.head = this.tail;
	this.tail = t;

}

@javadsalgo123,
change the while loop condition to curr.next!=null.

Also, share the complete code if your query is not resolved.

@javadsalgo123,
What doubt are you having in this?

reverse pointer function is not working

@javadsalgo123,

Your logic was right. You missed a line in the code. You had to include the line this.tail.next = null; at last.

not working , still can’t figure out why ?

@javadsalgo123,
Can you share the question you are attempting?

@javadsalgo123,
In the code I shared.

For the input:
5
1 2 3 4 5

Output should be: (when printing data)
before reversing
1=>2=>3=>4=>5=>end
after reversing
5=>4=>3=>2=>1=>end

Input
5
1 2 3 4 5

Output (when printing nodes. The output will vary from system to system):
before reversing
Main$Node@6aaa5eb0=>Main$Node@3498ed=>Main$Node@1a407d53=>Main$Node@3d8c7aca=>Main$Node@5ebec15=>end
after reversing
Main$Node@5ebec15=>Main$Node@3d8c7aca=>Main$Node@1a407d53=>Main$Node@3498ed=>Main$Node@6aaa5eb0=>end

I am not able to display the linked list again , i don’t know why?


here is the code , please help me out =>https://ide.codingblocks.com/s/252594

@javadsalgo123,

Comment the print in the while loop in reversepointer method. Put 2 displays method, one before reversepointer and one after reversepointer. Call the reversepointer method again. And share a screenshot of that.

I am executing the same code I sent to you. Also, please share a screenshot of the method from which you are calling the reversePointer.


I am unable to print the list again

@javadsalgo123,
In the reverse pointer function:
Use

		while (curr != null) {

Instead of:

		while (curr.next != null) {

I made a mistake myself when I told you edit that. It was correct. :sweat_smile: The code should work now.


please check the console .

@javadsalgo123,
the output looks correct, except that the tail element is not being displayed. Can you send me your display function please?


public void display() {
Node temp = this.head;
System.out.println("head "+this.head.data);
System.out.println("tail "+this.tail.data);
System.out.println(“size “+this.size);
while (temp.next != null) {
System.out.print(temp.data + “=>”);
temp = temp.next;
}
System.out.println(“end”);
System.out.println(”============================”);
}

@javadsalgo123,
Don’t use

while (temp.next != null) {

just put:

while(temp!=null)

In the display function

@javadsalgo123,
Because if the next in a node is null, we can still print its data but if the node is null, that’s when we have to exit.

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.