25
Oct
Reverse linked list using pointers
Related Blog Items
- Reversing a circular single linked list using pointers
- Reversing a double linked list
- Reversing a single linked list using stack
- Reversing single linked list recursively
- Questions asked in various job interviews (in Programming)
Reversing a single list can achieved using a stack very easily. however this can be done without using stack, and by simply using three additional pointers. Let us say we are using curr, next, result pointers, curr points to current node, next obviously points to the next node, result points to the new reversed linked list.
here is the pictorial representation of this operation for better understanding…

now result holds one reversed node, and curr points to the rest of the linked list..

now result holds a pointer to the reversed linked list (reversed two pointers already)…. curr holds rest of the list
we reversed successfully….
here is the routine for doing this…..
void reverse_single_linked_list(struct node** headRef) { struct node* result = NULL; struct node* current = *headRef; struct node* next; while (current != NULL) { next = current->next; // tricky: note the next node current->next = result; // move the node onto the result result = current; current = next; } *headRef = result; }
please let me know your comments….
Popularity: 94%
You need to log on to convert this article into PDF
Related Blog Items - Reversing a circular single linked list using pointers
- Reversing a double linked list
- Reversing a single linked list using stack
- Reversing single linked list recursively
- Questions asked in various job interviews (in Programming)
Related Blog Items
- Reversing a circular single linked list using pointers
- Reversing a double linked list
- Reversing a single linked list using stack
- Reversing single linked list recursively
- Questions asked in various job interviews (in Programming)
[…] Reversing a single linked list is explained in this post […]
March 10th, 2007 at 4:50 pm
[…] Here is the routine for reversing single linked list recursively, for reversing iteratively see the earlier post . For reversing circular single linked list visit this post. This routine takes a single linked list as argument and reverse it and returns back the head node to the reversed list. […]
March 30th, 2007 at 3:30 pm
[…] Here is the routine for reversing single linked list using a stack, for reversing linked list using pointers, see the earlier post. For reversing linked list recursively visit this post. This routine takes a single linked list as argument and reverse it and returns back the head node to the reversed list. […]
March 30th, 2007 at 3:40 pm
wow! pretty illustrative, I can only say keep the good work bro.
May 18th, 2007 at 12:48 pm
This site so far concentrated on linked lists in data structures, is there any chance of writing about other data structures in the same depth…
December 19th, 2007 at 8:50 pm
I wrote an implementation of the LinkedList class in JavaScript using the MooTools library. I also wrote a challenge and solution to reversing a singly linked list:
http://www.thetruetribe.com/2008/05/javascript-challenge-reverse-linked.html
May 14th, 2008 at 4:09 pm