cpppro

Archive for October, 2006|Monthly archive page

Reverse linked list using pointers

In C Tidbits, Data Structures in C/C++ on October 25, 2006 at 6:22 pm

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…
SLL_reverse_1
now result holds one reversed node, and curr points to the rest of the linked list..
SLL_reverse_2

now result holds a pointer to the reversed linked list (reversed two pointers already)…. curr holds rest of the list
SLL_reverse_3

we reversed successfully….
SLL_reverse_4

here is the routine for doing this…..

please refer http://www.refcode.net/2013/02/reverse-linked-list-using-pointers.html
Advertisements