0ms C solution,My code maybe looks complicated but very effective, you can make it simple.


  • 0
    C
    struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
    struct ListNode *p1,*p2;
    p1=head;
    struct ListNode *t=p1->next;
    if(head==NULL)return head;
    int i;
    while(t!=NULL)
    {
        i=0;
        p2=p1;
        p1=p1->next;
        t=p1;
        for(;i<n;i++)
        {
           if(t==NULL)break;
           t=t->next;
        }
    }
    if(p1==head || i!=n)return head->next;
    p2->next=p1->next;
    return head
    

    }


Log in to reply
 

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