Reverse Single Linked List

How will you reverse a single linked list which consists of nodes a to z using recursion?

Questions by shyamkumar1221

Showing Answers 1 - 10 of 10 Answers

manubn

  • Nov 15th, 2010
 

Function : 


// Returns - the pointer to the starting node of the reversed linked list.
// Parameters - Previous node, Current node

Node* listReverse( Node* prev, Node* ptr )
{
   Node* res = NULL;
   if( NULL == ptr )
   {
    res = prev;
   }
   else
   {
    res = listReverse( ptr, ptr->next );
    ptr->next = prev;
   }
   return res;
}

I hope this function suffices the question.

Usage: 

Node* prev = NULL;
Node* reversedList = listReverse( prev, ptr );// ptr is the start of the linked list.

The above two answers have bugs... They don't work correctly... Here is the code


struct node* reverse(node *head)
{
   node *current=NULL;
  if(head->next==NULL)
  {
      //we have only one element in our list
      current=head;
  }
  else
 {
    current=reverse(head->next);
    head->next->next=head;
    head->next=NULL;
 }
 return current;
}

  Was this answer useful?  Yes

shri

  • Jul 15th, 2011
 

//hope this may work

Code
  1. struct node* reverse(node *head)

  2. {

  3.    node *current=NULL;

  4.   if(head->next==NULL)

  5.   {

  6.       //we have only one element in our list

  7.       current=head;

  8.       return current;

  9.   }

  10.   else

  11.  {

  12.     current=reverse(head->next);

  13.     current->next=head;

  14.     head->next=NULL;

  15.    return current->next;

  16. }

  17.  

  Was this answer useful?  Yes

It works in gcc compiler

Code
  1. struct node

  2. {

  3.         int data;

  4.         struct node *link;

  5. };

  6. struct node * reverse(struct node *root,struct node *next)

  7. {

  8.         struct node *ret=NULL;

  9.         if(root==NULL)

  10.                 return NULL;

  11.         else

  12.         if(root->link!=NULL)

  13.                 ret=reverse(root->link,root);

  14.         else

  15.         if(root->link==NULL)

  16.                 ret=root;

  17.         root->link=next;

  18.         return ret;

  19. }

  20.  

  21.  

  22. //call tot he function

  23.         struct node *dummy=NULL;

  24.         head=reverse(head,dummy);

  25.  

  Was this answer useful?  Yes

ilushka

  • Jul 29th, 2011
 

Lets see...

Code
  1. node *reverse(node *list) {

  2.   node *reversed_last = NULL;

  3.  

  4.   if (list->next != NULL) {

  5.     reversed_last = reverse(list->next);

  6.   } else {

  7.     return list;

  8.   }

  9.  

  10.   reversed_last->next = list;

  11.   list->next = NULL;

  12.  

  13.   return list;

  14. }

  Was this answer useful?  Yes

sasibhushan.m

  • Jul 30th, 2011
 

node* reverse_recursive(node *root)
{
if(root->link != NULL)
{
reverse_recursive(root->link);
root->link->link = root;
return (root);
}
else
{
head = root;
}
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions