diff --git a/Source codes/Data Structures/C++/remove_nth_node_from_endofthe_list.cpp b/Source codes/Data Structures/C++/remove_nth_node_from_endofthe_list.cpp new file mode 100644 index 0000000..2b4941e --- /dev/null +++ b/Source codes/Data Structures/C++/remove_nth_node_from_endofthe_list.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + ListNode* removeNthFromEnd(ListNode* head, int n) { + + int l=0; + ListNode *p=head; + + while(p!=nullptr) + { + l++; + p=p->next; + } + l=l-n; + if(l==0) + { + head=head->next; + return head; + } + p=head; + for(int i=0;i!=l-1;i++) + { + p=p->next; + } + p->next=p->next->next; + return head; + } +};