chore(CPlusPlus) : add reverse linked list (#942)

pull/963/merge
Pravar Anu 2022-10-06 23:01:13 +05:30 committed by GitHub
parent c63a39519a
commit 07c44c1843
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 98 additions and 1 deletions

View File

@ -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 <iostream>
#include <stack>
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<<endl;
Print(head);
return 0;
}
void Print(Node* n){
if(n==NULL){
return;
}
cout<<n->data<<" ";
Print(n->next);
}
void RevList() {
if(head == NULL) return;
stack<Node *> 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;
}

View File

@ -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)