-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_7.cpp
121 lines (106 loc) · 2.15 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
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
//
// 2_7.cpp
//
//
// Created by Pengyan Qin on 6/17/15.
//
//
#include <iostream>
#include <stack>
using namespace std;
struct node{
int key;
node* next;
node() {}
node(int x):key(x), next(nullptr){}
};
node* init(int a[], int n){
node* head = nullptr;
node* q = nullptr;
for(int i = 0; i < n; ++i){
node *p = new node;
p->key = a[i];
p->next = nullptr;
if(i == 0){
head = p;
q = p;
}
else{
q->next = p;
q = p;
}
}
return head;
}
void print(node* head){
node *walker = head;
while(walker->next){
cout << walker->key << " ";
walker = walker->next;
}
cout << walker->key << endl;
return;
}
// use stack
// time complexity: O(n)
// space complexity: O(n)
bool is_palindrome(node *head){
int length = 1;
node *cur = head;
while(cur->next){
length++;
cur = cur->next;
}
stack<int> s;
cur = head;
for(int i = 0; i < length/2; ++i){
s.push(cur->key);
cur = cur->next;
}
if(length%2)
cur = cur->next;
while(cur){
if(s.top() == cur->key){
s.pop();
cur = cur->next;
}
else
return false;
}
return true;
}
// take advantage of the feature of the palindrome, original = reverse
// time complexity: O(n)
// space complexity: O(n)
bool is_palindrome1(node *head){
node *cur = head;
node *rhead = nullptr;
while(cur){
node *ptr = new node(cur->key);
ptr->next = rhead;
rhead = ptr;
cur = cur->next;
}
while(rhead && head){
if(rhead->key == head->key){
rhead = rhead->next;
head = head->next;
}
else
return false;
}
return true;
}
int main(){
int a[] = {2, 3, 1, 1, 3, 2};
int n = 6;
int b[] = {2, 3, 1, 1, 3, 5};
int m = 6;
node* head1 = init(a, n);
node* head2 = init(b, m);
print(head1);
print(head2);
cout << is_palindrome1(head1) << endl;
cout << is_palindrome1(head2) << endl;
return 0;
}