Skip to content

Commit bf2c587

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0234
1 parent 90defe1 commit bf2c587

File tree

10 files changed

+152
-240
lines changed

10 files changed

+152
-240
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
1. [删除链表的节点](/lcof/面试题18.%20删除链表的节点/README.md)
6060
1. [链表中倒数第 k 个节点](/lcci/02.02.Kth%20Node%20From%20End%20of%20List/README.md)
6161
1. [反转链表](/solution/0200-0299/0206.Reverse%20Linked%20List/README.md)
62+
1. [回文链表](/solution/0200-0299/0234.Palindrome%20Linked%20List/README.md)
6263
1. [环形链表](/solution/0100-0199/0141.Linked%20List%20Cycle/README.md)
6364
1. [环形链表 II](/solution/0100-0199/0142.Linked%20List%20Cycle%20II/README.md)
6465

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
5757
1. [Delete Node in a Linked List](/solution/0200-0299/0237.Delete%20Node%20in%20a%20Linked%20List/README_EN.md)
5858
1. [Kth Node From End of List](/lcci/02.02.Kth%20Node%20From%20End%20of%20List/README_EN.md)
5959
1. [Reverse Linked List](/solution/0200-0299/0206.Reverse%20Linked%20List/README_EN.md)
60+
1. [Palindrome Linked List](/solution/0200-0299/0234.Palindrome%20Linked%20List/README_EN.md)
6061
1. [Linked List Cycle](/solution/0100-0199/0141.Linked%20List%20Cycle/README_EN.md)
6162
1. [Linked List Cycle II](/solution/0100-0199/0142.Linked%20List%20Cycle%20II/README_EN.md)
6263

lcci/02.06.Palindrome Linked List/README.md

+16-21
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
<!-- 这里可写通用的实现逻辑 -->
3232

33-
先利用快慢指针找到链表中点,之后将后半部分链表利用头插法逆序,再比对前后两段链表得出结果
33+
先用快慢指针找到链表的中点,接着反转右半部分的链表。然后同时遍历前后两段链表,若前后两段链表节点对应的值不等,说明不是回文链表,否则说明是回文链表
3434

3535
<!-- tabs:start -->
3636

@@ -44,28 +44,22 @@
4444
# def __init__(self, x):
4545
# self.val = x
4646
# self.next = None
47-
4847
class Solution:
4948
def isPalindrome(self, head: ListNode) -> bool:
5049
if head is None or head.next is None:
5150
return True
5251
slow, fast = head, head.next
5352
while fast and fast.next:
54-
fast = fast.next.next
55-
slow = slow.next
56-
cur = slow.next
57-
slow.next = None
53+
slow, fast = slow.next, fast.next.next
54+
pre, cur = None, slow.next
5855
while cur:
5956
t = cur.next
60-
cur.next = slow.next
61-
slow.next = cur
62-
cur = t
63-
t = slow.next
64-
while t:
65-
if head.val != t.val:
57+
cur.next = pre
58+
pre, cur = cur, t
59+
while pre:
60+
if pre.val != head.val:
6661
return False
67-
t = t.next
68-
head = head.next
62+
pre, head = pre.next, head.next
6963
return True
7064
```
7165

@@ -87,26 +81,27 @@ class Solution {
8781
if (head == null || head.next == null) {
8882
return true;
8983
}
90-
ListNode slow = head, fast = head.next;
84+
ListNode slow = head;
85+
ListNode fast = head.next;
9186
while (fast != null && fast.next != null) {
9287
slow = slow.next;
9388
fast = fast.next.next;
9489
}
9590
ListNode cur = slow.next;
9691
slow.next = null;
92+
ListNode pre = null;
9793
while (cur != null) {
9894
ListNode t = cur.next;
99-
cur.next = slow.next;
100-
slow.next = cur;
95+
cur.next = pre;
96+
pre = cur;
10197
cur = t;
10298
}
103-
ListNode t = slow.next;
104-
while (t != null) {
105-
if (head.val != t.val) {
99+
while (pre != null) {
100+
if (pre.val != head.val) {
106101
return false;
107102
}
103+
pre = pre.next;
108104
head = head.next;
109-
t = t.next;
110105
}
111106
return true;
112107
}

lcci/02.06.Palindrome Linked List/README_EN.md

+15-20
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,22 @@ Could you do it in O(n) time and O(1) space?</p>
4646
# def __init__(self, x):
4747
# self.val = x
4848
# self.next = None
49-
5049
class Solution:
5150
def isPalindrome(self, head: ListNode) -> bool:
5251
if head is None or head.next is None:
5352
return True
5453
slow, fast = head, head.next
5554
while fast and fast.next:
56-
fast = fast.next.next
57-
slow = slow.next
58-
cur = slow.next
59-
slow.next = None
55+
slow, fast = slow.next, fast.next.next
56+
pre, cur = None, slow.next
6057
while cur:
6158
t = cur.next
62-
cur.next = slow.next
63-
slow.next = cur
64-
cur = t
65-
t = slow.next
66-
while t:
67-
if head.val != t.val:
59+
cur.next = pre
60+
pre, cur = cur, t
61+
while pre:
62+
if pre.val != head.val:
6863
return False
69-
t = t.next
70-
head = head.next
64+
pre, head = pre.next, head.next
7165
return True
7266
```
7367

@@ -87,26 +81,27 @@ class Solution {
8781
if (head == null || head.next == null) {
8882
return true;
8983
}
90-
ListNode slow = head, fast = head.next;
84+
ListNode slow = head;
85+
ListNode fast = head.next;
9186
while (fast != null && fast.next != null) {
9287
slow = slow.next;
9388
fast = fast.next.next;
9489
}
9590
ListNode cur = slow.next;
9691
slow.next = null;
92+
ListNode pre = null;
9793
while (cur != null) {
9894
ListNode t = cur.next;
99-
cur.next = slow.next;
100-
slow.next = cur;
95+
cur.next = pre;
96+
pre = cur;
10197
cur = t;
10298
}
103-
ListNode t = slow.next;
104-
while (t != null) {
105-
if (head.val != t.val) {
99+
while (pre != null) {
100+
if (pre.val != head.val) {
106101
return false;
107102
}
103+
pre = pre.next;
108104
head = head.next;
109-
t = t.next;
110105
}
111106
return true;
112107
}

lcci/02.06.Palindrome Linked List/Solution.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,27 @@ public boolean isPalindrome(ListNode head) {
1111
if (head == null || head.next == null) {
1212
return true;
1313
}
14-
ListNode slow = head, fast = head.next;
14+
ListNode slow = head;
15+
ListNode fast = head.next;
1516
while (fast != null && fast.next != null) {
1617
slow = slow.next;
1718
fast = fast.next.next;
1819
}
1920
ListNode cur = slow.next;
2021
slow.next = null;
22+
ListNode pre = null;
2123
while (cur != null) {
2224
ListNode t = cur.next;
23-
cur.next = slow.next;
24-
slow.next = cur;
25+
cur.next = pre;
26+
pre = cur;
2527
cur = t;
2628
}
27-
ListNode t = slow.next;
28-
while (t != null) {
29-
if (head.val != t.val) {
29+
while (pre != null) {
30+
if (pre.val != head.val) {
3031
return false;
3132
}
33+
pre = pre.next;
3234
head = head.next;
33-
t = t.next;
3435
}
3536
return true;
3637
}

lcci/02.06.Palindrome Linked List/Solution.py

+7-13
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,20 @@
33
# def __init__(self, x):
44
# self.val = x
55
# self.next = None
6-
76
class Solution:
87
def isPalindrome(self, head: ListNode) -> bool:
98
if head is None or head.next is None:
109
return True
1110
slow, fast = head, head.next
1211
while fast and fast.next:
13-
fast = fast.next.next
14-
slow = slow.next
15-
cur = slow.next
16-
slow.next = None
12+
slow, fast = slow.next, fast.next.next
13+
pre, cur = None, slow.next
1714
while cur:
1815
t = cur.next
19-
cur.next = slow.next
20-
slow.next = cur
21-
cur = t
22-
t = slow.next
23-
while t:
24-
if head.val != t.val:
16+
cur.next = pre
17+
pre, cur = cur, t
18+
while pre:
19+
if pre.val != head.val:
2520
return False
26-
t = t.next
27-
head = head.next
21+
pre, head = pre.next, head.next
2822
return True

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

+36-60
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
<!-- 这里可写通用的实现逻辑 -->
2727

28+
先用快慢指针找到链表的中点,接着反转右半部分的链表。然后同时遍历前后两段链表,若前后两段链表节点对应的值不等,说明不是回文链表,否则说明是回文链表。
29+
2830
<!-- tabs:start -->
2931

3032
### **Python3**
@@ -37,38 +39,23 @@
3739
# def __init__(self, x):
3840
# self.val = x
3941
# self.next = None
40-
4142
class Solution:
4243
def isPalindrome(self, head: ListNode) -> bool:
43-
if not head:
44+
if head is None or head.next is None:
4445
return True
45-
mid = self.find_mid_node(head)
46-
second_half_list = self.reverse_list(mid.next)
47-
result = True
48-
p, q = head, second_half_list
49-
while result and q:
50-
if p.val != q.val:
51-
result = False
52-
else:
53-
p, q = p.next, q.next
54-
mid.next = self.reverse_list(second_half_list)
55-
return result
56-
57-
def reverse_list(self, head):
58-
pre, p = None, head
59-
while p:
60-
q = p.next
61-
p.next = pre
62-
pre = p
63-
p = q
64-
return pre
65-
66-
def find_mid_node(self, head):
67-
slow = fast = head
68-
while fast.next and fast.next.next:
46+
slow, fast = head, head.next
47+
while fast and fast.next:
6948
slow, fast = slow.next, fast.next.next
70-
return slow
71-
49+
pre, cur = None, slow.next
50+
while cur:
51+
t = cur.next
52+
cur.next = pre
53+
pre, cur = cur, t
54+
while pre:
55+
if pre.val != head.val:
56+
return False
57+
pre, head = pre.next, head.next
58+
return True
7259
```
7360

7461
### **Java**
@@ -86,43 +73,32 @@ class Solution:
8673
*/
8774
class Solution {
8875
public boolean isPalindrome(ListNode head) {
89-
if (head == null) {
76+
if (head == null || head.next == null) {
9077
return true;
9178
}
92-
ListNode mid = findMidNode(head);
93-
ListNode secondHalfList = reverseList(mid.next);
94-
boolean result = true;
95-
ListNode p = head, q = secondHalfList;
96-
while (result && q != null) {
97-
if (p.val != q.val) {
98-
result = false;
99-
} else {
100-
p = p.next;
101-
q = q.next;
102-
}
103-
}
104-
mid.next = reverseList(secondHalfList);
105-
return result;
106-
}
107-
108-
private ListNode reverseList(ListNode head) {
109-
ListNode pre = null, p = head;
110-
while (p != null) {
111-
ListNode q = p.next;
112-
p.next = pre;
113-
pre = p;
114-
p = q;
115-
}
116-
return pre;
117-
}
118-
119-
private ListNode findMidNode(ListNode head) {
120-
ListNode slow = head, fast = head;
121-
while (fast.next != null && fast.next.next != null) {
79+
ListNode slow = head;
80+
ListNode fast = head.next;
81+
while (fast != null && fast.next != null) {
12282
slow = slow.next;
12383
fast = fast.next.next;
12484
}
125-
return slow;
85+
ListNode cur = slow.next;
86+
slow.next = null;
87+
ListNode pre = null;
88+
while (cur != null) {
89+
ListNode t = cur.next;
90+
cur.next = pre;
91+
pre = cur;
92+
cur = t;
93+
}
94+
while (pre != null) {
95+
if (pre.val != head.val) {
96+
return false;
97+
}
98+
pre = pre.next;
99+
head = head.next;
100+
}
101+
return true;
126102
}
127103
}
128104
```

0 commit comments

Comments
 (0)