-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode234.cpp
76 lines (69 loc) · 2.06 KB
/
leetcode234.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
/**
* linked list真的不能倒著遍歷嗎?
*/
class Solution {
public:
ListNode* left;
// 由於stack的特性
// res第一次終止(也就是right == NULL)之後
// 會倒著遍歷linked list
bool traverse(ListNode* right) {
if(right == NULL)
return true;
bool res = traverse(right->next);
res = res && (left->val == right->val);
left = left->next;
return res;
}
bool isPalindrome(ListNode* head) {
if(head == NULL)
return true;
left = head;
return traverse(head);
}
};
/**
* 快慢指針:快指針一次移動兩步,慢指針一次移動ㄧ步
* 從慢指針開始反轉鏈表
* 但是有分兩種情況
* 若鏈表有奇數個node,快指針會指向最後一個,慢指針會指向中間往右一位 (把中心留在左邊)
* 若有偶數個node,快指針會指向NULL,慢指針會指向n/2 + 1
*/
class Solution {
public:
ListNode* reverse(ListNode* head) {
// pre -> cur -> nxt
// NULL -> head -> head.next
ListNode* pre = NULL;
ListNode* cur = head;
while(cur != NULL) {
ListNode* nxt = cur->next;
cur->next = pre;
pre = cur;
cur = nxt;
}
return pre;
}
bool isPalindrome(ListNode* head) {
ListNode *slow, *fast;
slow = fast = head;
while(fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
// 鏈表有奇數個node,中心要留在左邊,所以慢指針再往右一位
if(fast != NULL)
slow = slow->next;
// 左指針是左邊的鏈表頭
ListNode* left = head;
// 右指針是右邊的鏈表頭
ListNode* right = reverse(slow);
while(right != NULL) {
if(left->val != right->val)
return false;
left = left->next;
right = right->next;
}
return true;
}
};