Skip to content

Commit db05338

Browse files
committed
234 Palindrome Linked List
1 parent c579477 commit db05338

File tree

3 files changed

+196
-0
lines changed

3 files changed

+196
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
+ [230 Kth Smallest Element in a BST(BST、中序遍历)](algorithms/KthSmallestElementinaBST)
130130
+ [231 Power of Two(位运算,二进制1的个数)](algorithms/PowerofTwo)
131131
+ [232 Implement Queue using Stacks(栈模拟队列)](algorithms/ImplementQueueusingStacks)
132+
+ [234 Palindrome Linked List(链表逆转、回文链表)](algorithms/PalindromeLinkedList)
132133
+ [235 Lowest Common Ancestor of a Binary Search Tree(LCA, 最低公共祖先,二叉搜索树)](algorithms/LowestCommonAncestorofaBinarySearchTree)
133134
+ [236 Lowest Common Ancestor of a Binary Tree(LCA,最低公共祖先,二叉树](algorithms/LowestCommonAncestorofaBinaryTree)
134135
+ [237 Delete Node in a Linked List(O(1)删除单链表节点)](algorithms/DeleteNodeinaLinkedList)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
## Palindrome Linked List
2+
3+
Given a singly linked list, determine if it is a palindrome.
4+
5+
Follow up:
6+
Could you do it in O(n) time and O(1) space?
7+
8+
## Solution
9+
10+
找到中间节点,然后原地逆转右半部分,比如左半部分和右半部分是否相等
11+
12+
首先实现找到中间节点,使用快慢指针:
13+
14+
```cpp
15+
ListNode *getMiddleNode(ListNode *head) {
16+
if (head == nullptr)
17+
return nullptr;
18+
ListNode *slow = head, *fast = head;
19+
while (fast && fast->next) {
20+
fast = fast->next->next;
21+
slow = slow->next;
22+
}
23+
return slow;
24+
}
25+
```
26+
27+
逆转链表:
28+
29+
```cpp
30+
ListNode *reverse(ListNode *head) {
31+
if (head == nullptr || head->next == nullptr)
32+
return head;
33+
ListNode *prev = nullptr;
34+
ListNode *p = head;
35+
while (p) {
36+
ListNode *q = p->next;
37+
p->next = prev;
38+
prev = p;
39+
p = q;
40+
}
41+
return prev;
42+
}
43+
```
44+
45+
最后结果:
46+
47+
```cpp
48+
bool isPalindrome(ListNode *head) {
49+
/* 空节点和单节点 */
50+
if (head == nullptr || head->next == nullptr)
51+
return true;
52+
ListNode *mid = getMiddleNode(head);
53+
ListNode *h1 = head;
54+
ListNode *h2 = reverse(mid->next);
55+
while(h1 && h2) {
56+
if (h1->val != h2->val)
57+
return false;
58+
h1 = h1->next;
59+
h2 = h2->next;
60+
}
61+
return true;
62+
63+
}
64+
```
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <string>
6+
using namespace std;
7+
#include <stdlib.h>
8+
struct ListNode {
9+
int val;
10+
ListNode *next;
11+
ListNode(int x): val(x), next(nullptr) {}
12+
};
13+
class Solution {
14+
public:
15+
bool isPalindrome(ListNode *head) {
16+
/* 空节点和单节点 */
17+
if (head == nullptr || head->next == nullptr)
18+
return true;
19+
ListNode *mid = getMiddleNode(head);
20+
ListNode *h1 = head;
21+
ListNode *h2 = reverse(mid->next);
22+
while(h1 && h2) {
23+
if (h1->val != h2->val)
24+
return false;
25+
h1 = h1->next;
26+
h2 = h2->next;
27+
}
28+
return true;
29+
30+
}
31+
private:
32+
ListNode *getMiddleNode(ListNode *head) {
33+
if (head == nullptr)
34+
return nullptr;
35+
ListNode *slow = head, *fast = head;
36+
while (fast && fast->next) {
37+
fast = fast->next->next;
38+
slow = slow->next;
39+
}
40+
return slow;
41+
}
42+
ListNode *reverse(ListNode *head) {
43+
if (head == nullptr || head->next == nullptr)
44+
return head;
45+
ListNode *prev = nullptr;
46+
ListNode *p = head;
47+
while (p) {
48+
ListNode *q = p->next;
49+
p->next = prev;
50+
prev = p;
51+
p = q;
52+
}
53+
return prev;
54+
}
55+
int getLength(ListNode *head) {
56+
int size = 0;
57+
ListNode *p = head;
58+
while(p) {
59+
size++;
60+
p = p->next;
61+
}
62+
return size;
63+
}
64+
65+
};
66+
int getLength(ListNode *head)
67+
{
68+
int len = 0;
69+
ListNode *p = head;
70+
while (p) {
71+
++len;
72+
p = p->next;
73+
}
74+
return len;
75+
}
76+
void print(ListNode *head)
77+
{
78+
if (head == nullptr) {
79+
printf("NULL\n");
80+
return;
81+
}
82+
struct ListNode *p = head;
83+
while (p) {
84+
printf("%d ", p->val);
85+
p = p->next;
86+
}
87+
printf("\n");
88+
}
89+
ListNode * mk_list(int a[], int n)
90+
{
91+
if (n < 1)
92+
return nullptr;
93+
ListNode *head = new ListNode(a[0]);
94+
ListNode *p = head;
95+
for (int i = 1; i < n; ++i) {
96+
ListNode *q = new ListNode(a[i]);
97+
p->next = q;
98+
p = q;
99+
}
100+
return p;
101+
}
102+
ListNode * mk_list(const vector<int> &v) {
103+
int n = v.size();
104+
if (n < 1)
105+
return nullptr;
106+
ListNode *head = new ListNode(v[0]);
107+
ListNode *p = head;
108+
for (int i = 1; i < n; ++i) {
109+
ListNode *q = new ListNode(v[i]);
110+
p->next = q;
111+
p = q;
112+
}
113+
return head;
114+
}
115+
116+
void free_list(struct ListNode *head)
117+
{
118+
struct ListNode *p = head;
119+
while (p) {
120+
struct ListNode *q = p->next;
121+
delete p;
122+
p = q;
123+
}
124+
}
125+
int main(int argc, char **argv)
126+
{
127+
Solution solution;
128+
struct ListNode *head = mk_list({1,2,2,1});
129+
cout << solution.isPalindrome(head) << endl;
130+
return 0;
131+
}

0 commit comments

Comments
 (0)