forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Palinas.cpp
57 lines (48 loc) · 1.08 KB
/
Palinas.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
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
int Solution::lPalin(ListNode* A) {
ListNode* curr = A;
ListNode* mid = A;
ListNode* prev;
ListNode* newCurr;
ListNode* newTemp;
ListNode* newPrev;
if(curr == NULL){
return 1;
}
if(curr->next == NULL){
return 1;
}
while((curr != NULL) && (curr->next != NULL)){
curr = (curr->next)->next;
prev = mid;
mid = mid->next;
}
prev->next = NULL;
newCurr = mid;
while(newCurr != NULL){
newTemp = newCurr->next;
if(newCurr == mid){
newCurr->next = NULL;
newPrev = newCurr;
newCurr = newTemp;
}
else{
newCurr->next = newPrev;
newPrev = newCurr;
newCurr = newTemp;
}
}
curr = A;
while((newPrev != NULL) && (curr != NULL)){
if(newPrev->val != curr->val){
return 0;
}
newPrev = newPrev->next;
curr = curr->next;
}
return 1;
}