Will this recursive solution be a one pass? Please give me your thoughts


  • 0
    S
    void ParseList(struct ListNode *ptr,struct ListNode *head, struct ListNode *prev, int n, int *reverseCount)
    {
        struct ListNode *temp;
            if(ptr == NULL)
            {
                (*reverseCount)++;
                return;
            }
    
            ParseList(ptr->next, head, ptr, n, reverseCount);
    
            if((*reverseCount) == n)
            {
                if(ptr == head->next)
                {
                    head->next = ptr->next;
                }
                else
                    prev->next = ptr->next;
            }
    
            (*reverseCount)++;
    
    }
    
    struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
    
        int reverseCount = 0;
        struct ListNode *prev = NULL, *dummyHead;
        dummyHead = (struct ListNode *)(malloc(sizeof(struct ListNode *)));
        dummyHead->next = head;
        if(head->next == NULL)
        {
            return NULL;
        }
        else if(head)
            ParseList(head, dummyHead, prev, n, &reverseCount);
    
        head = dummyHead->next;
        free(dummyHead);
        return head;
    }
    

Log in to reply
 

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