Skip to content

Commit

Permalink
Create remove Nth node from the end of linked list.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
SouparnaChatterjee authored Oct 21, 2023
1 parent d666b45 commit 1a14537
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions remove Nth node from the end of linked list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* start= new ListNode;
start->next= head;
ListNode* fast=start;
ListNode* slow=start;
for(int i=0;i<n;i++)
fast=fast->next;
while(fast->next!=NULL)
{
fast=fast->next;
slow=slow->next;
}
slow->next=slow->next->next;
return start->next;
}
};

0 comments on commit 1a14537

Please sign in to comment.