Skip to content

Commit b6ac462

Browse files
committed
feat: add python and java solutions to lcci question
添加《程序员面试金典》题解:面试题 02.06. 回文链表
1 parent 775e755 commit b6ac462

File tree

4 files changed

+260
-50
lines changed

4 files changed

+260
-50
lines changed

lcci/02.06.Palindrome Linked List/README.md

+66-3
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,83 @@
2626

2727
## 解法
2828
<!-- 这里可写通用的实现逻辑 -->
29-
29+
先利用快慢指针找到链表中点,之后将后半部分链表利用头插法逆序,再比对前后两段链表得出结果。
3030

3131
### Python3
3232
<!-- 这里可写当前语言的特殊实现逻辑 -->
3333

3434
```python
35-
35+
# Definition for singly-linked list.
36+
# class ListNode:
37+
# def __init__(self, x):
38+
# self.val = x
39+
# self.next = None
40+
41+
class Solution:
42+
def isPalindrome(self, head: ListNode) -> bool:
43+
if head is None or head.next is None:
44+
return True
45+
slow, fast = head, head.next
46+
while fast and fast.next:
47+
fast = fast.next.next
48+
slow = slow.next
49+
cur = slow.next
50+
slow.next = None
51+
while cur:
52+
t = cur.next
53+
cur.next = slow.next
54+
slow.next = cur
55+
cur = t
56+
t = slow.next
57+
while t:
58+
if head.val != t.val:
59+
return False
60+
t = t.next
61+
head = head.next
62+
return True
3663
```
3764

3865
### Java
3966
<!-- 这里可写当前语言的特殊实现逻辑 -->
4067

4168
```java
42-
69+
/**
70+
* Definition for singly-linked list.
71+
* public class ListNode {
72+
* int val;
73+
* ListNode next;
74+
* ListNode(int x) { val = x; }
75+
* }
76+
*/
77+
class Solution {
78+
public boolean isPalindrome(ListNode head) {
79+
if (head == null || head.next == null) {
80+
return true;
81+
}
82+
ListNode slow = head, fast = head.next;
83+
while (fast != null && fast.next != null) {
84+
slow = slow.next;
85+
fast = fast.next.next;
86+
}
87+
ListNode cur = slow.next;
88+
slow.next = null;
89+
while (cur != null) {
90+
ListNode t = cur.next;
91+
cur.next = slow.next;
92+
slow.next = cur;
93+
cur = t;
94+
}
95+
ListNode t = slow.next;
96+
while (t != null) {
97+
if (head.val != t.val) {
98+
return false;
99+
}
100+
head = head.next;
101+
t = t.next;
102+
}
103+
return true;
104+
}
105+
}
43106
```
44107

45108
### ...
+129-47
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,129 @@
1-
# [02.06. Palindrome Linked List](https://leetcode-cn.com/problems/palindrome-linked-list-lcci)
2-
3-
## Description
4-
<p>Implement a function to check if a linked list is a palindrome.</p>
5-
6-
<p>&nbsp;</p>
7-
8-
<p><strong>Example 1: </strong></p>
9-
10-
<pre>
11-
<strong>Input: </strong>1-&gt;2
12-
<strong>Output: </strong> false
13-
</pre>
14-
15-
<p><strong>Example 2: </strong></p>
16-
17-
<pre>
18-
<strong>Input: </strong>1-&gt;2-&gt;2-&gt;1
19-
<strong>Output: </strong> true
20-
</pre>
21-
22-
<p>&nbsp;</p>
23-
24-
<p><b>Follow up:</b><br />
25-
Could you do it in O(n) time and O(1) space?</p>
26-
27-
28-
29-
## Solutions
30-
31-
32-
### Python3
33-
34-
```python
35-
36-
```
37-
38-
### Java
39-
40-
```java
41-
42-
```
43-
44-
### ...
45-
```
46-
47-
```
1+
# [02.06. Palindrome Linked List](https://leetcode-cn.com/problems/palindrome-linked-list-lcci)
2+
3+
## Description
4+
<p>Implement a function to check if a linked list is a palindrome.</p>
5+
6+
7+
8+
<p>&nbsp;</p>
9+
10+
11+
12+
<p><strong>Example 1: </strong></p>
13+
14+
15+
16+
<pre>
17+
18+
<strong>Input: </strong>1-&gt;2
19+
20+
<strong>Output: </strong> false
21+
22+
</pre>
23+
24+
25+
26+
<p><strong>Example 2: </strong></p>
27+
28+
29+
30+
<pre>
31+
32+
<strong>Input: </strong>1-&gt;2-&gt;2-&gt;1
33+
34+
<strong>Output: </strong> true
35+
36+
</pre>
37+
38+
39+
40+
<p>&nbsp;</p>
41+
42+
43+
44+
<p><b>Follow up:</b><br />
45+
46+
Could you do it in O(n) time and O(1) space?</p>
47+
48+
## Solutions
49+
50+
51+
### Python3
52+
53+
```python
54+
# Definition for singly-linked list.
55+
# class ListNode:
56+
# def __init__(self, x):
57+
# self.val = x
58+
# self.next = None
59+
60+
class Solution:
61+
def isPalindrome(self, head: ListNode) -> bool:
62+
if head is None or head.next is None:
63+
return True
64+
slow, fast = head, head.next
65+
while fast and fast.next:
66+
fast = fast.next.next
67+
slow = slow.next
68+
cur = slow.next
69+
slow.next = None
70+
while cur:
71+
t = cur.next
72+
cur.next = slow.next
73+
slow.next = cur
74+
cur = t
75+
t = slow.next
76+
while t:
77+
if head.val != t.val:
78+
return False
79+
t = t.next
80+
head = head.next
81+
return True
82+
```
83+
84+
### Java
85+
86+
```java
87+
/**
88+
* Definition for singly-linked list.
89+
* public class ListNode {
90+
* int val;
91+
* ListNode next;
92+
* ListNode(int x) { val = x; }
93+
* }
94+
*/
95+
class Solution {
96+
public boolean isPalindrome(ListNode head) {
97+
if (head == null || head.next == null) {
98+
return true;
99+
}
100+
ListNode slow = head, fast = head.next;
101+
while (fast != null && fast.next != null) {
102+
slow = slow.next;
103+
fast = fast.next.next;
104+
}
105+
ListNode cur = slow.next;
106+
slow.next = null;
107+
while (cur != null) {
108+
ListNode t = cur.next;
109+
cur.next = slow.next;
110+
slow.next = cur;
111+
cur = t;
112+
}
113+
ListNode t = slow.next;
114+
while (t != null) {
115+
if (head.val != t.val) {
116+
return false;
117+
}
118+
head = head.next;
119+
t = t.next;
120+
}
121+
return true;
122+
}
123+
}
124+
```
125+
126+
### ...
127+
```
128+
129+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public boolean isPalindrome(ListNode head) {
11+
if (head == null || head.next == null) {
12+
return true;
13+
}
14+
ListNode slow = head, fast = head.next;
15+
while (fast != null && fast.next != null) {
16+
slow = slow.next;
17+
fast = fast.next.next;
18+
}
19+
ListNode cur = slow.next;
20+
slow.next = null;
21+
while (cur != null) {
22+
ListNode t = cur.next;
23+
cur.next = slow.next;
24+
slow.next = cur;
25+
cur = t;
26+
}
27+
ListNode t = slow.next;
28+
while (t != null) {
29+
if (head.val != t.val) {
30+
return false;
31+
}
32+
head = head.next;
33+
t = t.next;
34+
}
35+
return true;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def isPalindrome(self, head: ListNode) -> bool:
9+
if head is None or head.next is None:
10+
return True
11+
slow, fast = head, head.next
12+
while fast and fast.next:
13+
fast = fast.next.next
14+
slow = slow.next
15+
cur = slow.next
16+
slow.next = None
17+
while cur:
18+
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:
25+
return False
26+
t = t.next
27+
head = head.next
28+
return True

0 commit comments

Comments
 (0)