Code for Reversing a Linked List

by Dinesh 2013-12-24 18:54:14

Code for Reversing a Linked List


void reverse()
{
if(head == null)
;
Node p = head.next, q = head, r;
while ( p != null )
{
r = q; // r follows q
q = p; // q follows p
p = p.next; // p moves to next node
q.next = r; // link q to preceding node
}
head.next = null;
head = q;
}

Tagged in:

599
like
0
dislike
0
mail
flag

You must LOGIN to add comments