Skip to content

Commit 257f428

Browse files
committed
added palindrome and DLL reverse for linked list
1 parent a1202e2 commit 257f428

File tree

2 files changed

+222
-0
lines changed

2 files changed

+222
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <iostream>
4+
using namespace std;
5+
6+
struct Node
7+
{
8+
int data;
9+
struct Node *next;
10+
Node(int x)
11+
{
12+
data = x;
13+
next = NULL;
14+
}
15+
};
16+
17+
class Solution
18+
{
19+
public:
20+
//Function to check whether the list is palindrome.
21+
Node *reverseList(Node *head)
22+
{
23+
Node *curr = head;
24+
Node *temp = NULL;
25+
Node *prev = NULL;
26+
while (curr)
27+
{
28+
temp = curr->next;
29+
curr->next = prev;
30+
prev = curr;
31+
curr = temp;
32+
}
33+
34+
return prev;
35+
}
36+
bool isPalindrome(Node *head)
37+
{
38+
Node *slow = head;
39+
Node *fast = head;
40+
while (fast && fast->next)
41+
{
42+
fast = fast->next->next;
43+
slow = slow->next;
44+
}
45+
slow = reverseList(slow);
46+
fast = head;
47+
while (slow)
48+
{
49+
if (slow->data != fast->data)
50+
{
51+
return false;
52+
}
53+
slow = slow->next;
54+
fast = fast->next;
55+
}
56+
57+
return true;
58+
//Your code here
59+
}
60+
};
61+
62+
int main()
63+
{
64+
int T, i, n, l, firstdata;
65+
cin >> T;
66+
while (T--)
67+
{
68+
69+
struct Node *head = NULL, *tail = NULL;
70+
cin >> n;
71+
// taking first data of LL
72+
cin >> firstdata;
73+
head = new Node(firstdata);
74+
tail = head;
75+
// taking remaining data of LL
76+
for (i = 1; i < n; i++)
77+
{
78+
cin >> l;
79+
tail->next = new Node(l);
80+
tail = tail->next;
81+
}
82+
Solution obj;
83+
cout << obj.isPalindrome(head) << endl;
84+
}
85+
return 0;
86+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
struct Node
6+
{
7+
int data;
8+
Node * next;
9+
Node * prev;
10+
Node (int x)
11+
{
12+
data=x;
13+
next=NULL;
14+
prev=NULL;
15+
}
16+
17+
};
18+
19+
Node *newNode(int data)
20+
{
21+
Node *temp=new Node(data);
22+
23+
return temp;
24+
}
25+
26+
27+
28+
29+
void displayList(Node *head)
30+
{
31+
while(head->next)
32+
{
33+
cout<<head->data<<" ";
34+
head=head->next;
35+
}
36+
cout<<head->data;
37+
38+
39+
40+
}
41+
42+
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+
81+
82+
// main function for reversing the doubly linked list
83+
Node* reverseDLL(Node * head)
84+
{
85+
Node* temp = NULL;
86+
Node* curr = head;
87+
while(curr){
88+
temp = curr->prev;
89+
curr->prev=curr->next;
90+
curr->next = temp;
91+
curr=curr->prev;
92+
}
93+
94+
if(temp){
95+
head= temp->prev;
96+
}
97+
98+
return head;
99+
}
100+
101+
102+
103+
int main() {
104+
int t;
105+
cin>>t;
106+
while(t--)
107+
{
108+
int n;
109+
cin>>n;
110+
Node *head=NULL, *tail=NULL;
111+
int x;
112+
cin>>x;
113+
head = newNode(x);
114+
tail = head;
115+
116+
for(int i=0;i<n - 1;i++)
117+
{
118+
cin>>x;
119+
Node* temp=newNode(x);
120+
tail->next=temp;
121+
temp->prev= tail;
122+
tail = temp;
123+
}
124+
head=reverseDLL(head);
125+
126+
127+
if(verify(head))
128+
displayList(head);
129+
else
130+
cout<<"Your pointers are not correctly connected";
131+
132+
cout<<endl;
133+
}
134+
return 0;
135+
}
136+

0 commit comments

Comments
 (0)