How to recursively reverse a circular linked list?

Viewed 38
Node* reverseRecursive(Node* node)
{
    if(node->next == head ) {
        return node;
    }

    auto res = reverseRecursive(node->next);
    node->next->next = node;
    // one line is missing here
    return res;
}

This is what I have done so far.

1 Answers

I think I found the solution:

Node* reverseRecursive(Node* node) {
    if(node->next == head ) {
        return node;
    }

    auto res = reverseRecursive(node->next);
    node->next->next = node;
    node->next = res;
    return res;
}

Assigning the next pointer to the last node was that remaining line .

Related