Monday, 16 May 2011

Write a recursive approach to reverse a link list.


void recursiveReverse(NODE** headNode)
{
NODE* firstNode;
NODE* restOfNodes;

if(headNode == NULL)
{
printf("List is Empty\n");
return;
}

firstNode = *headNode;
restOfNodes = firstNode->next;

if (restOfNodes == NULL)
{
return;
}

recursiveReverse(&restOfNodes);

firstNode->next->next = firstNode;
firstNode->next = NULL;

*headNode = restOfNodes;
}

No comments:

Post a Comment