From fa30bd9396a7ec62e3ee29b50986c81ff6396960 Mon Sep 17 00:00:00 2001 From: Pranav Malhotra Date: Wed, 30 Oct 2024 15:27:49 +0530 Subject: [PATCH] Remove Nth node from the end of list --- .../Remove Nth node from the end.cpp" | 83 +++++++++++++++++++ .../question.md" | 4 + "Beginner Level \360\237\223\201/data.json" | 5 +- 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 "Beginner Level \360\237\223\201/Remove Nth node from end of list/Remove Nth node from the end.cpp" create mode 100644 "Beginner Level \360\237\223\201/Remove Nth node from end of list/question.md" diff --git "a/Beginner Level \360\237\223\201/Remove Nth node from end of list/Remove Nth node from the end.cpp" "b/Beginner Level \360\237\223\201/Remove Nth node from end of list/Remove Nth node from the end.cpp" new file mode 100644 index 00000000..ef52ff1d --- /dev/null +++ "b/Beginner Level \360\237\223\201/Remove Nth node from end of list/Remove Nth node from the end.cpp" @@ -0,0 +1,83 @@ +#include +using namespace std; + +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) { + if (head == nullptr) { + return head; + } + ListNode* dummy = new ListNode(0, head); + ListNode* first = dummy; + ListNode* second = dummy; + + for (int i = 0; i <= n; i++) { + first = first->next; + } + + while (first != nullptr) { + first = first->next; + second = second->next; + } + + ListNode* nodeToDelete = second->next; + second->next = nodeToDelete->next; + delete nodeToDelete; + + ListNode* newHead = dummy->next; + delete dummy; + return newHead; + } +}; + +void printList(ListNode* head) { + ListNode* current = head; + while (current != nullptr) { + cout << current->val << " -> "; + current = current->next; + } + cout << "nullptr" << endl; +} + +ListNode* createList(int arr[], int size) { + if (size == 0) return nullptr; + ListNode* head = new ListNode(arr[0]); + ListNode* current = head; + for (int i = 1; i < size; i++) { + current->next = new ListNode(arr[i]); + current = current->next; + } + return head; +} + +int main() { + int arr[] = {1, 2, 3, 4, 5}; + ListNode* head = createList(arr, 5); + + cout << "Original List: "; + printList(head); + + Solution solution; + int n = 2; + head = solution.removeNthFromEnd(head, n); + + cout << "List after removing " << n << "th node from the end: "; + printList(head); + + ListNode* temp; + while (head != nullptr) { + temp = head; + head = head->next; + delete temp; + } + + return 0; +} diff --git "a/Beginner Level \360\237\223\201/Remove Nth node from end of list/question.md" "b/Beginner Level \360\237\223\201/Remove Nth node from end of list/question.md" new file mode 100644 index 00000000..a64e5eb6 --- /dev/null +++ "b/Beginner Level \360\237\223\201/Remove Nth node from end of list/question.md" @@ -0,0 +1,4 @@ +Given the head of a linked list, remove the nth node from the end of the list and return its head. + +Input: head = [1,2,3,4,5], n = 2 +Output: [1,2,3,5] \ No newline at end of file diff --git "a/Beginner Level \360\237\223\201/data.json" "b/Beginner Level \360\237\223\201/data.json" index 23297d68..f7acbbf4 100644 --- "a/Beginner Level \360\237\223\201/data.json" +++ "b/Beginner Level \360\237\223\201/data.json" @@ -145,6 +145,9 @@ "name": "Vimal M", "githubUsername": "vimall03" }, - + { + "name": "Pranav Malhotra", + "githubUsername": "Pranav-malhotra" + }, ]