Play and learn 300 000+ tabs online

Saturday, June 19, 2010

Reverse a Linkedlist in recursion

void reverse(list *l)
{
    lnode *temp;
    temp=l->first;
    while(temp->next != NULL)
    {
         temp=temp->next;
     }
      while(temp !=NULL)
      {
           printf("%d",temp->info);
           temp=temp->prev;
       }
}




/* Reverse linked list by recursion */

if(head)
    head = _ReverseLinkedList(NULL, head, head->next);

NodeStr    *_ReverseLinkedList(
          NodeStr    *preNode,
          NodeStr    *node1,
          NodeStr       *node2
          )
{
    NodeStr        *next_node;

    if(!node2)
        return node1;

    next_node = node2->next;
    node2->next = node1;
    node1->next = preNode;
    return _ReverseLinkedList(node1, node2, next_node);
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.