Reversing a linkedlist recursively in c

后端 未结 9 1245
滥情空心
滥情空心 2020-12-07 23:33

The following code works fine when head is sent as a parameter to it. As I am new to C, I couldn\'t understand how it works. Help me out please.

struct node          


        
相关标签:
9条回答
  • 2020-12-08 00:05

    Another solution:

    struct node *reverse_recur(struct node *temp)
    {
        if(temp->link==NULL)
        {
            return temp;
        }
    
        struct node *temp1=temp->link;
    
        temp->link=NULL;
    
        return (reverse_recur(temp1)->link=temp);
    
    }
    
    0 讨论(0)
  • 2020-12-08 00:13
    ll *rev_list(ll *prev, ll *cur)
    {
        if (!cur) {
            return prev;
        }
    
        ll *tmp = cur;
        cur = cur->next;
        tmp->next = prev;
        prev = tmp;
        return rev_list(prev, cur);
    }
    

    Find complete code : https://github.com/vijaythreadtemp/Data-Structures-And-Algorithms/blob/master/rev_link_list_rec.cxx

    0 讨论(0)
  • 2020-12-08 00:14
    /* Reverses a linked list, returns head of reversed list
    */
    NodePtr reverseList(NodePtr curr) {
        if (curr == NULL || curr->next == NULL) return curr; // empty or single element case
    
        NodePtr nextElement = curr->next;
        curr->next = NULL;
        NodePtr head = reverseList(nextElement);
        nextElement->next = curr;
        return head;
    }
    
    0 讨论(0)
提交回复
热议问题