Skip to content

Commit c288a04

Browse files
committed
added desc. in reverse doubly LL
1 parent 835ccde commit c288a04

File tree

1 file changed

+17
-45
lines changed

1 file changed

+17
-45
lines changed

Data Structures/Linked Lists/reverse_a_double_linkedlist.cpp

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
#include <bits/stdc++.h>
22

3+
/*
4+
Given a doubly linked list of n elements. The task is to reverse the doubly linked list.
5+
6+
Example:
7+
Input: LinkedList: 3 <--> 4 <--> 5
8+
Output: 5 4 3
9+
*/
10+
311
using namespace std;
412

513
struct Node
@@ -24,8 +32,6 @@ Node *newNode(int data)
2432
}
2533

2634

27-
28-
2935
void displayList(Node *head)
3036
{
3137
while(head->next)
@@ -39,47 +45,16 @@ void displayList(Node *head)
3945

4046
}
4147

48+
// main function for reversing the doubly linked list
4249

43-
int getLength(Node * head)
44-
{
45-
Node *temp=head;
46-
47-
int count=0;
48-
while(temp->next!=head)
49-
{
50-
count++;
51-
temp=temp->next;
52-
}
53-
return count+1;
54-
}
55-
56-
57-
58-
59-
bool verify(Node* head)
60-
{
61-
int fl=0;
62-
int bl=0;
63-
64-
Node *temp=head;
65-
66-
while(temp->next)
67-
{
68-
temp=temp->next;
69-
fl++;
70-
}
71-
72-
while(temp->prev)
73-
{
74-
temp=temp->prev;
75-
bl++;
76-
}
77-
78-
return fl==bl;
79-
}
80-
50+
/*
51+
Approch:
52+
Simply just keep track of the previous node
53+
and the next node.
8154
82-
// main function for reversing the doubly linked list
55+
Almost similar to the singly linked list you have an extra pointer
56+
as prev to take care of
57+
*/
8358
Node* reverseDLL(Node * head)
8459
{
8560
Node* temp = NULL;
@@ -124,11 +99,8 @@ int main() {
12499
head=reverseDLL(head);
125100

126101

127-
if(verify(head))
128102
displayList(head);
129-
else
130-
cout<<"Your pointers are not correctly connected";
131-
103+
132104
cout<<endl;
133105
}
134106
return 0;

0 commit comments

Comments
 (0)