From 1a1453752c572e0988049c3921d69195e0aee6e8 Mon Sep 17 00:00:00 2001 From: SouparnaChatterjee <115988950+SouparnaChatterjee@users.noreply.github.com> Date: Sat, 21 Oct 2023 11:44:30 +0530 Subject: [PATCH] Create remove Nth node from the end of linked list.cpp --- ...e Nth node from the end of linked list.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 remove Nth node from the end of linked list.cpp diff --git a/remove Nth node from the end of linked list.cpp b/remove Nth node from the end of linked list.cpp new file mode 100644 index 0000000000..96cf94485d --- /dev/null +++ b/remove Nth node from the end of linked list.cpp @@ -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;inext; + while(fast->next!=NULL) + { + fast=fast->next; + slow=slow->next; + } + slow->next=slow->next->next; + return start->next; + } +};