-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
80 lines (74 loc) · 1.83 KB
/
main.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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
//Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
// 用vector存储链表中的值,然后再判断回文
bool isPalindrome(ListNode* head) {
ListNode *cur;
cur = head;
vector<int> res;
while(cur != NULL){
res.push_back(cur->val);
cur = cur->next;
}
for(int i=0; i<res.size(); i++){
if(res[i] != res[res.size()-i-1]){ //判断回文
return false;
}
}
return true;
}
//将链表后半部分进行反转,然后再进行回文判断
//设置快慢指针,都从head开始遍历,fast每次走两步,slow每次走一步
//当fast遍历结束时,slow刚好指在中间的位置
bool isPalindrome1(ListNode* head) {
if(head == NULL || head->next == NULL){
return true;
}
ListNode *slow,*fast,*cur;
slow = head;
fast = head;
while(fast->next !=NULL && fast ->next ->next != NULL){
slow = slow->next;
fast = fast->next->next;
}
slow -> next = reverseList(slow->next);
slow = slow -> next;
cur = head;
while(slow != NULL){
if(cur->val != slow->val){
return false;
}
cur = cur->next;
slow = slow->next;
}
return true;
}
//链表逆置
ListNode *reverseList(ListNode* head){
ListNode *cur,*pre;
pre = NULL;
cur = head;
while(cur != NULL){
ListNode *temp = cur -> next;
cur -> next = pre;
pre = cur;
cur = temp;
}
return pre;
}
};
int main()
{
return 0;
}