Accept C in 2ms


  • 0
    A
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    struct ListNode* then(struct ListNode *head,int *k,int n);
    
    struct ListNode *removeNthFromEnd(struct ListNode *head, int n) {
        int k=0;
        struct ListNode begin,*temp;
        begin.val=-1;
        begin.next=head;
        struct ListNode *ans=then(&begin,&k,n);
        ans->next=ans->next->next;
        return begin.next;
    }
    
    struct ListNode* then(struct ListNode *head,int *k,int n){
        if(head->next!=NULL){
            struct ListNode *ans=then(head->next,k,n);
            *k += 1;
            if(*k==n)
                return head;
            else
                return ans;
        }
        else{
            *k = 0;
            return NULL;
        }
    }

Log in to reply
 

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