-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathis_sll_palindromic.cpp
More file actions
118 lines (98 loc) · 2.31 KB
/
Copy pathis_sll_palindromic.cpp
File metadata and controls
118 lines (98 loc) · 2.31 KB
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
//is SLL palindromic
//https://ide.geeksforgeeks.org/OrwKA8z84i
#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
struct Node* next;
};
void printLL(struct Node* head){
if(head == NULL) return;
while(head!=NULL){
cout<<head->data<<" ";
head = head->next;
}
cout<<"\n";
}
int countLL(struct Node *head){
if(head == NULL) return 0;
int cnt = 1;
while(head){
cnt++;
head = head->next;
}
return cnt;
}
Node* newNode(int key)
{
Node* temp = new Node;
temp->data = key;
temp->next = NULL;
return temp;
}
Node* reverseLL(Node *head){
//return head;
}
void isSLLPalindromic(Node *head){
if(head == NULL) return;
Node *traverse = head;
Node *c1 = head;
int total = countLL(traverse);
int mid = total/2;
if(mid%2 == 0) mid = mid -1;
for(int i=1; i<=mid; i++){
if(traverse == NULL) return;
traverse = traverse->next;
}
if(traverse == NULL) return;
Node *subHead = traverse->next;
Node *prev = NULL;
Node *next;
Node *curr = subHead;
while(curr != NULL){
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
subHead = prev;
Node *c2 = subHead;
bool flag = true;
for(int i=1; i<=mid; i++){
if(c1->data != c2->data){
flag = false;
}
c1 = c1->next;
c2 = c2->next;
}
if(!flag){
cout<<"Not palindrome"<<"\n";
}
else{
cout<<"Palindrome"<<"\n";
}
//printLL(head);
}
int main()
{
Node* head1 = newNode(1);
head1->next = newNode(2);
head1->next->next = newNode(3);
head1->next->next->next = newNode(4);
head1->next->next->next->next = newNode(3);
head1->next->next->next->next->next = newNode(2);
head1->next->next->next->next->next->next = newNode(1);
Node* head2 = newNode(1);
head2->next = newNode(2);
head2->next->next = newNode(3);
head2->next->next->next = newNode(4);
head2->next->next->next->next = newNode(3);
head2->next->next->next->next->next = newNode(2);
head2->next->next->next->next->next->next = newNode(5);
printLL(head1);
isSLLPalindromic(head1);
cout<<"\n";
printLL(head2);
isSLLPalindromic(head2);
return 0;
}