-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalindromelink.cpp
63 lines (59 loc) · 1.28 KB
/
Palindromelink.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
#include<vector>
#include<algorithm>
#include<iostream>
typedef struct ListNode {
int val;
ListNode *next;
} s_ListNode;
/**
class Solution {
public:
bool isPalindrome(s_ListNode* head) {
s_ListNode *p = head;
std::vector<int> tmp;
tmp.reserve(100);
bool flag = false;
if (p==NULL||p->next==NULL)
return true;
while(p!=NULL){
if(tmp.size()==0){
tmp.push_back(p->val);
}
else if(p->val==tmp.back()){
tmp.pop_back();
if(tmp.size()==0)
return true;
}
else{
tmp.push_back(p->val);
}
p=p->next;
}
return false;
}
};
**/
class Solution{
public:
bool isPalindrome(s_ListNode* head){
std::vector<int> tmp;
tmp.reserve(100);
s_ListNode *p = head;
if(p==NULL)
return true;
while(p!=NULL){
tmp.push_back(p->val);
p=p->next;
}
p = head;
while(p!=NULL){
if(p->val!=tmp.back())
return false;
else{
tmp.pop_back();
}
p=p->next;
}
return true;
}
};