Click to See Complete Forum and Search --> : reversing linked list


Alpha Vijayan
May 27th, 2004, 05:31 AM
How to reverse a linked list with minimum time complexity??

yiannakop
May 27th, 2004, 08:27 AM
If it is a double linked list then simply:
a) swap head tail of list
b) swap next and previous pointers of each node

If it is a single linked list then sth like this (C - like pseudocode):


current_node = root;
new = NULL;

while (current_node!=NULL)
temp = current_node.next;
current_node.next = new;
new = current_node;
current_node = temp;
end_while

return new;