Skip to content

Commit eab71c0

Browse files
authored
Merge pull request #89 from version0chiro/Linked_List_Questions
added palindrome and DLL reverse for linked list
2 parents a1202e2 + c288a04 commit eab71c0

File tree

2 files changed

+205
-0
lines changed

2 files changed

+205
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <iostream>
4+
using namespace std;
5+
6+
7+
/*
8+
Approch used:
9+
-First traverse the LL to the middle node
10+
using floyd's algorithm.
11+
-Than reverse the second half of the Linked list.
12+
-Reset the fast pointer to head and compare each node.
13+
14+
-If Any does not match, the LL is not a palindrome.
15+
16+
17+
*/
18+
struct Node
19+
{
20+
int data;
21+
struct Node *next;
22+
Node(int x)
23+
{
24+
data = x;
25+
next = NULL;
26+
}
27+
};
28+
29+
class Solution
30+
{
31+
public:
32+
//Function to check whether the list is palindrome.
33+
Node *reverseList(Node *head)
34+
{
35+
Node *curr = head;
36+
Node *temp = NULL;
37+
Node *prev = NULL;
38+
while (curr)
39+
{
40+
temp = curr->next;
41+
curr->next = prev;
42+
prev = curr;
43+
curr = temp;
44+
}
45+
46+
return prev;
47+
}
48+
bool isPalindrome(Node *head)
49+
{
50+
Node *slow = head;
51+
Node *fast = head;
52+
while (fast && fast->next)
53+
{
54+
fast = fast->next->next;
55+
slow = slow->next;
56+
}
57+
slow = reverseList(slow);
58+
fast = head;
59+
while (slow)
60+
{
61+
if (slow->data != fast->data)
62+
{
63+
return false;
64+
}
65+
slow = slow->next;
66+
fast = fast->next;
67+
}
68+
69+
return true;
70+
}
71+
};
72+
73+
int main()
74+
{
75+
int T, i, n, l, firstdata;
76+
cin >> T;
77+
while (T--)
78+
{
79+
80+
struct Node *head = NULL, *tail = NULL;
81+
cin >> n;
82+
// taking first data of LL
83+
cin >> firstdata;
84+
head = new Node(firstdata);
85+
tail = head;
86+
// taking remaining data of LL
87+
for (i = 1; i < n; i++)
88+
{
89+
cin >> l;
90+
tail->next = new Node(l);
91+
tail = tail->next;
92+
}
93+
Solution obj;
94+
cout << obj.isPalindrome(head) << endl;
95+
}
96+
return 0;
97+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include <bits/stdc++.h>
2+
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+
11+
using namespace std;
12+
13+
struct Node
14+
{
15+
int data;
16+
Node * next;
17+
Node * prev;
18+
Node (int x)
19+
{
20+
data=x;
21+
next=NULL;
22+
prev=NULL;
23+
}
24+
25+
};
26+
27+
Node *newNode(int data)
28+
{
29+
Node *temp=new Node(data);
30+
31+
return temp;
32+
}
33+
34+
35+
void displayList(Node *head)
36+
{
37+
while(head->next)
38+
{
39+
cout<<head->data<<" ";
40+
head=head->next;
41+
}
42+
cout<<head->data;
43+
44+
45+
46+
}
47+
48+
// main function for reversing the doubly linked list
49+
50+
/*
51+
Approch:
52+
Simply just keep track of the previous node
53+
and the next node.
54+
55+
Almost similar to the singly linked list you have an extra pointer
56+
as prev to take care of
57+
*/
58+
Node* reverseDLL(Node * head)
59+
{
60+
Node* temp = NULL;
61+
Node* curr = head;
62+
while(curr){
63+
temp = curr->prev;
64+
curr->prev=curr->next;
65+
curr->next = temp;
66+
curr=curr->prev;
67+
}
68+
69+
if(temp){
70+
head= temp->prev;
71+
}
72+
73+
return head;
74+
}
75+
76+
77+
78+
int main() {
79+
int t;
80+
cin>>t;
81+
while(t--)
82+
{
83+
int n;
84+
cin>>n;
85+
Node *head=NULL, *tail=NULL;
86+
int x;
87+
cin>>x;
88+
head = newNode(x);
89+
tail = head;
90+
91+
for(int i=0;i<n - 1;i++)
92+
{
93+
cin>>x;
94+
Node* temp=newNode(x);
95+
tail->next=temp;
96+
temp->prev= tail;
97+
tail = temp;
98+
}
99+
head=reverseDLL(head);
100+
101+
102+
displayList(head);
103+
104+
cout<<endl;
105+
}
106+
return 0;
107+
}
108+

0 commit comments

Comments
 (0)