Skip to content

Latest commit

 

History

History
32 lines (27 loc) · 604 Bytes

AnuNancy.md

File metadata and controls

32 lines (27 loc) · 604 Bytes

Q. Remove Nth Node From End of List

class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode* temp=head;

   int count=0;
    while(temp->next != NULL)
    {
      count=count+1;
        temp=temp->next;
    }
    
    count=count-n;
    int i=1;
    temp=head;
    while(i<=count)
    {
        temp=temp->next;
        i++;
        
    }
    if(count<0)
    { head=head->next;}
        else
    {temp->next=temp->next->next;
    }
    return head;
}

};