Tried reversing a linked list using recursion but it goes into infinite loop

The other functions are working fine including reversing linked lists using iterative method but when i try to do so using recursion then it goes into infinite loop and just prints the first 2 elements. In CB ide it shows TLE so it doesnt even print the first 2 elements.

Node* reverse(Node* node) 
    { 
        if (node == NULL) 
            return NULL; 
        if (node->next == NULL) { 
            head = node; 
            return node; 
        } 
        Node* node1 = reverse(node->next); 
        node1->next = node; 
        node->next = NULL; 
        return node; 
    } 

This is all good but i needed to know what’s going wrong in the one i wrote.

image

== NULL
nhi h
= NULL

1 Like