For each recursion, keep track of the current node and the front of the rest of the node. Come back when the rest is NULL. After returning to recursion, cancel the following "rest" field to indicate the current one. To track the new first node, the recursive function simply takes the old node back.
void recursive_reverse() { // driver for the recursive reverse function. // first is a data member of linked list that point to the first node of list. first = recursive_reverse(first, first->next); } Node* recursive_reverse(Node *current, Node *rest) { // if rest == NULL, the current must be the old last node, // which is also the new first node if (rest == NULL) return current; Node *new_first = recursive_reverse(current->next, rest->next); // rearrange pointers rest->next = current; current->next = NULL; // pass along the new first node return new_first; }
My initial implementation uses a clock node, so I have not tested the code here. Sorry Just read it as pseudo code.
source share