Share my c++ solution


  • 0
    Z
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        int pos=0;
        ListNode* tmp = new ListNode(0);
        tmp->next=head;
        ListNode* node = tmp;
        while(node!=NULL&&pos!=n)
        {
            node=node->next;
            pos++;
        }
        ListNode* cur=tmp;
        while(node->next!=NULL)
        {
            node=node->next;
            cur=cur->next;
        }
        ListNode* post;
        post=cur->next->next;
        cur->next=post;
        return tmp->next;
    }

Log in to reply
 

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