forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome_check.cpp
126 lines (102 loc) · 2.69 KB
/
palindrome_check.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Given a singly linked list of size N of integers. The task is to check if the given linked list is palindrome or not.
Expected Time Complexity: O(N)
Expected Auxialliary Space Usage: O(1) (ie, you should not use the recursive stack space as well)
Input:
First line of input contains number of testcases T. For each testcase, first line of input contains length of linked list N and next line contains N integers as data of linked list.
Output:
For each test case output will be 1 if the linked list is a palindrome else 0.
User Task:
The task is to complete the function isPalindrome() which takes head as reference as the only parameter and returns true or false if linked list is palindrome or not respectively.
Constraints:
1 <= T <= 103
1 <= N <= 50
Example:
Input:
2
3
1 2 1
4
1 2 3 4
Output:
1
0
Explanation:
Testcase 1: The given linked list is 1 2 1 , which is a pallindrome and Hence, the output is 1.
Testcase 2: The given linked list is 1 2 3 4 , which is not a pallindrome and Hence, the output is 0.
*/
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
struct node* next;
};
struct node* createList(struct node* head, int data){
struct node *tmp=(struct node*)malloc(sizeof(struct node));
tmp->data=data;
tmp->next=NULL;
if(head==NULL){
head=tmp;
return head;
}
struct node* p=head;
while(p->next!=NULL){
p=p->next;
}
p->next=tmp;
return head;
}
void disp(struct node* head){
struct node* p=head;
while(p!=NULL){
cout<<p->data<<" ";
p=p->next;
}
}
struct node* findMid(struct node* head){
struct node *slow=head, *fast=head;
while(fast!=NULL && fast->next!=NULL){
slow=slow->next;
fast=fast->next->next;
}
return slow;
};
struct node* reversal(struct node* head){
if(head==NULL) return head;
struct node *cur=head, *prev=head, *ahead=head->next;
prev->next=NULL;
cur=ahead;
while(ahead!=NULL){
ahead=ahead->next;
cur->next=prev;
prev=cur;
cur=ahead;
}
head=prev;
return head;
}
bool palindromeCheck(struct node *head){
struct node *mid=findMid(head);
struct node*last=reversal(mid);
while(head!=mid){
if(head->data!=last->data) return false;
head=head->next;
last=last->next;
}
return true;
}
int main(){
struct node* head=NULL;
int n;
cin>>n;
for(int i=0;i<n;i++){
int data;
cin>>data;
head=createList(head, data);
}
if(palindromeCheck(head)){
cout<<"\nLinked List is Palindrome\n";
} else {
cout<<"\nLinked List is not Palindrome\n";
}
}