NodeType *Reverse(NodeType *List)
{
NodeType *Head;
NodeType *Tail;
NodeType *RList;
if(List == NULL) // Base Case - the empty list
return (NULL);
else
{
Partition(List, &Head, &Tail); // Note we are passing pointers to Head and Tail, that is Handles
RList = Concat(Reverse(Tail), Head);
return (RList);
}
}
void Partition(NodeType *Lst, NodeType **Hd, NodeType **Tl)
{
if(Lst != NULL)
{
*Tl = Lst->next; // By using a handle we are actually changing what Tail in Reverse() is pointing to.
*Hd = Lst; // This will set Head in Reverse() pointing to the rest of the list
(*Hd)->next = NULL; // This will set the next pointer in the node Head, in Reverse(), pointing to NULL
} // Note the use of parentheses due to binding of *
}
NodeType *Concat(Nodetype *L1, Nodetype *L2)
{ // L1 = reversed tail of a list, L2 = head of a list
NodeType *temp; // A search pointer used to find the end of list L1
if(L1 == NULL)
return (L2); // This is L2 concated to a NULL list
else
{
temp = L1;
while(temp->next != NULL)
temp = temp->next; // Search for end of list L1
temp->next = L2; // Concatenate L2 to end of L1
return L1;
}
}