Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix : add reverse doubly ll function #388

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@
| Helen Sahith Sadhe |<a href="https://github.com/helensahith">helensahith</a>|
| Vanshika Rathod |<a href="https://github.com/vanshikaaaaaaaa">vanshika</a>|
| Sujit Manojrao Nirmal |<a href="https://github.com/Blacksujit">helensahith</a>|
| Mahendra Pratap Verma |<a href="https://github.com/Mahendra2611">Mahendra2611</a>|

55 changes: 55 additions & 0 deletions c++/ReverseDoublyLL.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node* next = NULL;
Node* prev = NULL;
Node(int d){
this->data = d;
this->next = NULL;
this->prev = NULL;
}
};
void print(Node* &head){
Node* temp = head;
while(temp != NULL){
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
void insert(Node* &head,int d){
Node* temp = new Node(d);
temp->next = head;
head->prev = temp;
head = temp;
}
Node* reverse(Node* &head){
Node* temp = head;

while(temp != NULL){
Node* store = temp->next;
temp->next = temp->prev;
temp->prev = store;
head = temp;
temp = store;
}
//for using this condition we have to change print function temp = temp->next to temp = temp->prev
// while(temp != NULL){
// head = temp;
// temp = temp->next;
// }
return head;
}
int main(){
Node* node1 = new Node(7);
Node* head = node1;
insert(head,5);
insert(head,3);
insert(head,1);
print(head);
Node* result = reverse(head);
print(result);
return 0;
}