I know it is a minute code. I can’t understand why my linked list reversal is not working.
Can some one help fix me my reverse method in the below code.
//Reverse a single linked list
public Node reverse()
{
return reverse(root);
}
private Node reverse(Node node)
{
Node previous = null;
Node current = node;
Node forward;
while (current != null)
{
forward = current.next;
current.next = previous;
previous = current;
current = forward;
}
return previous;
}
Any input on this would be helpful
Thanks !!!
I’m pretty sure it should be
(Your reverse logic is correct, but if the
rootis still pointing at the old root of the list, you will end up with a 1-element linked list.)