Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

25
Oct

Reverse linked list using pointers

Related Blog Items

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…..

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: 90%

You need to log on to convert this article into PDF


Related Blog Items

6 Comments

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image