I talked about how to reverse a singly linked list earlier. That was slightly tricky to understand.
Reversing a doubly linked list is relatively easy. The logic is : You need to keep on changing the next and previous pointers as you traverse the entire list. Here is the code snippet in Java :
5 comments:
THX, was helpfull!!!
Hi,
This is the same as the reversal of the singly linked list.
I couldn't see the "previous" pointer/reference getting changed anywhere.
This method doesn't reverse. It only duplicates them.
pwnage zükerün
The previous pointer should also be updated as below
while (current != null)
{
DoubleNode next = current.next;
current.next = previous;
current.previous = next;
previous = current;
current = next;
}
Post a Comment