Iterative and recursion C solutions


  • 0
    A
    //iterative reverse list
    struct node* ItreverseList(struct node* head) {
        struct node* prev = NULL;
        struct node* current = head;
        while(current != NULL){
            struct node* next = current->next;
            current->next = prev;
            prev = current;
            current = next;
        }
        return prev;
    }
    
    //recursive reverse list
    struct node* RecreverseList(struct node* head) {
        if(head==NULL || head->next == NULL){
            return head;
        }
        struct node* newHead = RecreverseList(head->next);
        head->next->next = head;
        head->next = NULL;
        return newHead;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.