-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy path2-7.cpp
71 lines (56 loc) · 1.09 KB
/
2-7.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
//2.4 in 5th edtion
#include<iostream>
#include <stack>
using namespace std;
class ListNode{
public:
int val;
ListNode* next;
ListNode(int i): val(i),next(NULL){}
};
bool isp(ListNode* head,int len){
ListNode* p=head;
stack<int> st;
int i = len/2;
while (i>0){
st.push(p->val);
p=p->next;
i--;
}
if (len%2!=0){p=p->next;}
while (p){
if (p->val!=st.top()){return false;}
else{
st.pop();
p=p->next;
}
}
return true;
}
ListNode* creatl(int* A,int n){
ListNode* head = new ListNode(A[0]);
ListNode* q=head;
for (int i=1;i<n;i++){
ListNode* p=new ListNode(A[i]);
q->next = p;
q=p;
}
return head;
}
void printList(ListNode* head){
if (head==NULL){cout << "Empty List" << endl; }
while (head!=NULL){
cout << head->val << " ";
head = head->next;
}
cout << endl;
}
int main(){
int A[13] = {9,8,7,4,5,3,1,3,5,4,7,8,9};
ListNode* head = creatl(A,13);
printList(head);
int len = 13;
if (isp(head,13)){cout << "This is a palindrome"<<endl;}
else{cout << "This is NOT a palindrome"<<endl;}
return 0;
}