From 07c44c1843fa695874e97a66692b134c3feeba5b Mon Sep 17 00:00:00 2001 From: Pravar Anu <63355747+PravarAnu@users.noreply.github.com> Date: Thu, 6 Oct 2022 23:01:13 +0530 Subject: [PATCH] chore(CPlusPlus) : add reverse linked list (#942) --- .../reverse-the-list-using-stack.cpp | 97 +++++++++++++++++++ algorithms/CPlusPlus/README.md | 2 +- .../recursion/recursive-sum-of-n-numbers.py | 0 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 algorithms/CPlusPlus/Linked-Lists/reverse-the-list-using-stack.cpp rename recursive-sum-of-n-numbers.py => algorithms/Python/recursion/recursive-sum-of-n-numbers.py (100%) diff --git a/algorithms/CPlusPlus/Linked-Lists/reverse-the-list-using-stack.cpp b/algorithms/CPlusPlus/Linked-Lists/reverse-the-list-using-stack.cpp new file mode 100644 index 00000000..33121e63 --- /dev/null +++ b/algorithms/CPlusPlus/Linked-Lists/reverse-the-list-using-stack.cpp @@ -0,0 +1,97 @@ +/* +Algorithm: + (i) Traverse the list and push all of its nodes onto a stack. + (ii) Traverse the list from the head node again and pop a value + from the stack top and connect them in reverse order. + + +TIME COMPLEXITY: O(n), as we are traversing over the linked list of size N using a while loop. +SPACE COMPLEXITY: o(n), as we are using stack of size N in worst case which is extra space. + + */ + + +#include +#include + +using namespace std; + +class Node { +public: + int data; + Node *next; +}; +Node *head; + +void Print(Node* n); +void RevList(); + +int main() { + head = NULL; + + Node *first = new Node; + Node *second = new Node; + Node *third = new Node; + Node *fourth = new Node; + Node *fifth = new Node; + Node *sixth = new Node; + Node *seventh = new Node; + + head = first; + + first->data = 10; + first->next = second; + + second->data = 20; + second->next = third; + + third->data = 30; + third->next = fourth; + + fourth->data = 40; + fourth->next = fifth; + + fifth->data = 50; + fifth->next = sixth; + + sixth->data = 60; + sixth->next = seventh; + + seventh->data = 70; + seventh->next = NULL; + + Print(head); + + RevList(); + cout<data<<" "; + Print(n->next); +} +void RevList() { + if(head == NULL) return; + stack st; + Node * temp = head; + while(temp!= NULL){ + st.push(temp); + temp = temp->next; + } + temp = st.top(); + head = temp; + st.pop(); + + while(!st.empty()) { + temp->next = st.top(); + temp = temp->next; + st.pop(); + } + temp->next = NULL; +} diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index b9b71156..66505d55 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -78,7 +78,7 @@ - [Find Merge Point of two singly linked list](Linked-Lists/Find-Merge-Point.cpp) - [Segregate Even Odd Nodes of linked list](Linked-Lists/segregate-even-odd-nodes-of-linked-list.cpp) - [Remove Duplicate in Sorted linked list](Linked-Lists/remove-duplicates-in-sorted-linked-list.cpp) - +- [Reverse the linked list using stack](Linked-Lists/reverse-the-list-using-stack.cpp) ## Searching - [Linear Search](Searching/linear-search.cpp) diff --git a/recursive-sum-of-n-numbers.py b/algorithms/Python/recursion/recursive-sum-of-n-numbers.py similarity index 100% rename from recursive-sum-of-n-numbers.py rename to algorithms/Python/recursion/recursive-sum-of-n-numbers.py