Skip to content

Commit 254279a

Browse files
committed
feat: update solutions to lc problem: No.0234
No.0234.Palindrome Linked List
1 parent cc9c7fd commit 254279a

File tree

9 files changed

+19
-85
lines changed

9 files changed

+19
-85
lines changed

solution/0200-0299/0234.Palindrome Linked List/README.md

+9-27
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@
4141

4242
<!-- 这里可写通用的实现逻辑 -->
4343

44-
先用快慢指针找到链表的中点,接着反转右半部分的链表。然后同时遍历前后两段链表,若前后两段链表节点对应的值不等,说明不是回文链表,否则说明是回文链表。
44+
**方法一:快慢指针**
45+
46+
我们可以先用快慢指针找到链表的中点,接着反转右半部分的链表。然后同时遍历前后两段链表,若前后两段链表节点对应的值不等,说明不是回文链表,否则说明是回文链表。
47+
48+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为链表的长度。
4549

4650
<!-- tabs:start -->
4751

@@ -91,9 +95,6 @@ class Solution:
9195
*/
9296
class Solution {
9397
public boolean isPalindrome(ListNode head) {
94-
if (head == null || head.next == null) {
95-
return true;
96-
}
9798
ListNode slow = head;
9899
ListNode fast = head.next;
99100
while (fast != null && fast.next != null) {
@@ -137,7 +138,6 @@ class Solution {
137138
class Solution {
138139
public:
139140
bool isPalindrome(ListNode* head) {
140-
if (!head || !head->next) return true;
141141
ListNode* slow = head;
142142
ListNode* fast = head->next;
143143
while (fast && fast->next) {
@@ -173,9 +173,6 @@ public:
173173
* }
174174
*/
175175
func isPalindrome(head *ListNode) bool {
176-
if head == nil || head.Next == nil {
177-
return true
178-
}
179176
slow, fast := head, head.Next
180177
for fast != nil && fast.Next != nil {
181178
slow, fast = slow.Next, fast.Next.Next
@@ -213,9 +210,6 @@ func isPalindrome(head *ListNode) bool {
213210
* @return {boolean}
214211
*/
215212
var isPalindrome = function (head) {
216-
if (!head || !head.next) {
217-
return true;
218-
}
219213
let slow = head;
220214
let fast = head.next;
221215
while (fast && fast.next) {
@@ -258,31 +252,23 @@ var isPalindrome = function (head) {
258252
*/
259253
public class Solution {
260254
public bool IsPalindrome(ListNode head) {
261-
if (head == null || head.next == null)
262-
{
263-
return true;
264-
}
265255
ListNode slow = head;
266256
ListNode fast = head.next;
267-
while (fast != null && fast.next != null)
268-
{
257+
while (fast != null && fast.next != null) {
269258
slow = slow.next;
270259
fast = fast.next.next;
271260
}
272261
ListNode cur = slow.next;
273262
slow.next = null;
274263
ListNode pre = null;
275-
while (cur != null)
276-
{
264+
while (cur != null) {
277265
ListNode t = cur.next;
278266
cur.next = pre;
279267
pre = cur;
280268
cur = t;
281269
}
282-
while (pre != null)
283-
{
284-
if (pre.val != head.val)
285-
{
270+
while (pre != null) {
271+
if (pre.val != head.val) {
286272
return false;
287273
}
288274
pre = pre.next;
@@ -309,15 +295,12 @@ public class Solution {
309295
*/
310296

311297
function isPalindrome(head: ListNode | null): boolean {
312-
if (head == null || head.next == null) return true;
313-
// 快慢指针定位到中点
314298
let slow: ListNode = head,
315299
fast: ListNode = head.next;
316300
while (fast != null && fast.next != null) {
317301
slow = slow.next;
318302
fast = fast.next.next;
319303
}
320-
// 翻转链表
321304
let cur: ListNode = slow.next;
322305
slow.next = null;
323306
let prev: ListNode = null;
@@ -327,7 +310,6 @@ function isPalindrome(head: ListNode | null): boolean {
327310
prev = cur;
328311
cur = t;
329312
}
330-
// 判断回文
331313
while (prev != null) {
332314
if (prev.val != head.val) return false;
333315
prev = prev.next;

solution/0200-0299/0234.Palindrome Linked List/README_EN.md

+5-29
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@
4545
# self.val = val
4646
# self.next = next
4747
class Solution:
48-
def isPalindrome(self, head: ListNode) -> bool:
49-
if head is None or head.next is None:
50-
return True
48+
def isPalindrome(self, head: Optional[ListNode]) -> bool:
5149
slow, fast = head, head.next
5250
while fast and fast.next:
5351
slow, fast = slow.next, fast.next.next
@@ -78,9 +76,6 @@ class Solution:
7876
*/
7977
class Solution {
8078
public boolean isPalindrome(ListNode head) {
81-
if (head == null || head.next == null) {
82-
return true;
83-
}
8479
ListNode slow = head;
8580
ListNode fast = head.next;
8681
while (fast != null && fast.next != null) {
@@ -124,7 +119,6 @@ class Solution {
124119
class Solution {
125120
public:
126121
bool isPalindrome(ListNode* head) {
127-
if (!head || !head->next) return true;
128122
ListNode* slow = head;
129123
ListNode* fast = head->next;
130124
while (fast && fast->next) {
@@ -160,9 +154,6 @@ public:
160154
* }
161155
*/
162156
func isPalindrome(head *ListNode) bool {
163-
if head == nil || head.Next == nil {
164-
return true
165-
}
166157
slow, fast := head, head.Next
167158
for fast != nil && fast.Next != nil {
168159
slow, fast = slow.Next, fast.Next.Next
@@ -200,9 +191,6 @@ func isPalindrome(head *ListNode) bool {
200191
* @return {boolean}
201192
*/
202193
var isPalindrome = function (head) {
203-
if (!head || !head.next) {
204-
return true;
205-
}
206194
let slow = head;
207195
let fast = head.next;
208196
while (fast && fast.next) {
@@ -245,31 +233,23 @@ var isPalindrome = function (head) {
245233
*/
246234
public class Solution {
247235
public bool IsPalindrome(ListNode head) {
248-
if (head == null || head.next == null)
249-
{
250-
return true;
251-
}
252236
ListNode slow = head;
253237
ListNode fast = head.next;
254-
while (fast != null && fast.next != null)
255-
{
238+
while (fast != null && fast.next != null) {
256239
slow = slow.next;
257240
fast = fast.next.next;
258241
}
259242
ListNode cur = slow.next;
260243
slow.next = null;
261244
ListNode pre = null;
262-
while (cur != null)
263-
{
245+
while (cur != null) {
264246
ListNode t = cur.next;
265247
cur.next = pre;
266248
pre = cur;
267249
cur = t;
268250
}
269-
while (pre != null)
270-
{
271-
if (pre.val != head.val)
272-
{
251+
while (pre != null) {
252+
if (pre.val != head.val) {
273253
return false;
274254
}
275255
pre = pre.next;
@@ -296,15 +276,12 @@ public class Solution {
296276
*/
297277

298278
function isPalindrome(head: ListNode | null): boolean {
299-
if (head == null || head.next == null) return true;
300-
// 快慢指针定位到中点
301279
let slow: ListNode = head,
302280
fast: ListNode = head.next;
303281
while (fast != null && fast.next != null) {
304282
slow = slow.next;
305283
fast = fast.next.next;
306284
}
307-
// 翻转链表
308285
let cur: ListNode = slow.next;
309286
slow.next = null;
310287
let prev: ListNode = null;
@@ -314,7 +291,6 @@ function isPalindrome(head: ListNode | null): boolean {
314291
prev = cur;
315292
cur = t;
316293
}
317-
// 判断回文
318294
while (prev != null) {
319295
if (prev.val != head.val) return false;
320296
prev = prev.next;

solution/0200-0299/0234.Palindrome Linked List/Solution.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
class Solution {
1212
public:
1313
bool isPalindrome(ListNode* head) {
14-
if (!head || !head->next) return true;
1514
ListNode* slow = head;
1615
ListNode* fast = head->next;
1716
while (fast && fast->next) {

solution/0200-0299/0234.Palindrome Linked List/Solution.cs

+4-12
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,23 @@
1111
*/
1212
public class Solution {
1313
public bool IsPalindrome(ListNode head) {
14-
if (head == null || head.next == null)
15-
{
16-
return true;
17-
}
1814
ListNode slow = head;
1915
ListNode fast = head.next;
20-
while (fast != null && fast.next != null)
21-
{
16+
while (fast != null && fast.next != null) {
2217
slow = slow.next;
2318
fast = fast.next.next;
2419
}
2520
ListNode cur = slow.next;
2621
slow.next = null;
2722
ListNode pre = null;
28-
while (cur != null)
29-
{
23+
while (cur != null) {
3024
ListNode t = cur.next;
3125
cur.next = pre;
3226
pre = cur;
3327
cur = t;
3428
}
35-
while (pre != null)
36-
{
37-
if (pre.val != head.val)
38-
{
29+
while (pre != null) {
30+
if (pre.val != head.val) {
3931
return false;
4032
}
4133
pre = pre.next;

solution/0200-0299/0234.Palindrome Linked List/Solution.go

-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
* }
77
*/
88
func isPalindrome(head *ListNode) bool {
9-
if head == nil || head.Next == nil {
10-
return true
11-
}
129
slow, fast := head, head.Next
1310
for fast != nil && fast.Next != nil {
1411
slow, fast = slow.Next, fast.Next.Next

solution/0200-0299/0234.Palindrome Linked List/Solution.java

-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
*/
1111
class Solution {
1212
public boolean isPalindrome(ListNode head) {
13-
if (head == null || head.next == null) {
14-
return true;
15-
}
1613
ListNode slow = head;
1714
ListNode fast = head.next;
1815
while (fast != null && fast.next != null) {

solution/0200-0299/0234.Palindrome Linked List/Solution.js

-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
* @return {boolean}
1111
*/
1212
var isPalindrome = function (head) {
13-
if (!head || !head.next) {
14-
return true;
15-
}
1613
let slow = head;
1714
let fast = head.next;
1815
while (fast && fast.next) {

solution/0200-0299/0234.Palindrome Linked List/Solution.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
# self.val = val
55
# self.next = next
66
class Solution:
7-
def isPalindrome(self, head: ListNode) -> bool:
8-
if head is None or head.next is None:
9-
return True
7+
def isPalindrome(self, head: Optional[ListNode]) -> bool:
108
slow, fast = head, head.next
119
while fast and fast.next:
1210
slow, fast = slow.next, fast.next.next

solution/0200-0299/0234.Palindrome Linked List/Solution.ts

-4
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,12 @@
1111
*/
1212

1313
function isPalindrome(head: ListNode | null): boolean {
14-
if (head == null || head.next == null) return true;
15-
// 快慢指针定位到中点
1614
let slow: ListNode = head,
1715
fast: ListNode = head.next;
1816
while (fast != null && fast.next != null) {
1917
slow = slow.next;
2018
fast = fast.next.next;
2119
}
22-
// 翻转链表
2320
let cur: ListNode = slow.next;
2421
slow.next = null;
2522
let prev: ListNode = null;
@@ -29,7 +26,6 @@ function isPalindrome(head: ListNode | null): boolean {
2926
prev = cur;
3027
cur = t;
3128
}
32-
// 判断回文
3329
while (prev != null) {
3430
if (prev.val != head.val) return false;
3531
prev = prev.next;

0 commit comments

Comments
 (0)