From 703b0bfee53d7e5c89b859ecc93723e516ca055d Mon Sep 17 00:00:00 2001 From: ArhsiZahid Date: Wed, 13 Nov 2024 22:16:31 +0530 Subject: [PATCH] feat added (delete function in LinkedList --- data-structures/linkedList.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/data-structures/linkedList.cpp b/data-structures/linkedList.cpp index 33ef17b..47fa403 100644 --- a/data-structures/linkedList.cpp +++ b/data-structures/linkedList.cpp @@ -42,6 +42,33 @@ class LinkedList { } cout << endl; } + + void deleteNode(int targetValue) { + // check case 1 + if (head != NULL) { + // list is not empty + // need to traverse list, stopping at the last node + Node * currNode = head; + // check case 2... the node to delete is the first node + if (head->data == targetValue) { + head = head->next; + delete currNode; + } + else { // case 3... the node to delete is not the first node, but might not even be in the list + Node * prevNode = NULL; + while (currNode != NULL && currNode->data != targetValue) { + prevNode = currNode; + currNode = currNode->next; + } + // check if we found targetValue + if (currNode != NULL) { + // did find it + prevNode->next = currNode->next; + delete currNode; + } + } + } +} }; int main() { @@ -51,6 +78,7 @@ int main() { list.addNode(3); list.addNode(4); list.addNode(5); + list.deleteNode(2); list.printList(); return 0; } \ No newline at end of file