100%,c++,two pointer solution


  • 1
        ListNode* removeNthFromEnd(ListNode* head, int n) {
            ListNode *dummy=new ListNode(-1);
            dummy->next=head;
            ListNode *ptr1=dummy;
            ListNode *ptr2=head;
            while(n>0){
                ptr2=ptr2->next;
                n--;
            }
            while(ptr2!=NULL){
                ptr1=ptr1->next;
                ptr2=ptr2->next;
            }
            ptr1->next=ptr1->next->next;
            return dummy->next;
        }
    

Log in to reply
 

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