Skip to content

Commit 8c74d91

Browse files
committed
Add solution 234 [Java]
1 parent dd59cb7 commit 8c74d91

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
3636
| 198 | [House Robber](https://github.com/doocs/leetcode/tree/master/solution/198.House%20Robber) | `Dynamic Programming` |
3737
| 203 | [Remove Linked List Elements](https://github.com/doocs/leetcode/tree/master/solution/203.Remove%20Linked%20List%20Elements) | `Linked List` |
3838
| 231 | [Power of Two](https://github.com/doocs/leetcode/tree/master/solution/231.Power%20of%20Two) | `Math`, `Bit Manipulation` |
39+
| 234 | [Palindrome Linked List](https://github.com/doocs/leetcode/tree/master/solution/234.Palindrome%20Linked%20List) | `Linked List`, `Two Pointers` |
3940
| 235 | [Lowest Common Ancestor of a Binary Search Tree](https://github.com/doocs/leetcode/tree/master/solution/235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree) | `Tree` |
4041
| 237 | [Delete Node in a Linked List](https://github.com/doocs/leetcode/tree/master/solution/237.Delete%20Node%20in%20a%20Linked%20List) | `Linked List` |
4142
| 344 | [Reverse String](https://github.com/doocs/leetcode/tree/master/solution/344.Reverse%20String) | `Two Pointers`, `String` |
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
## 回文链表
2+
### 题目描述
3+
4+
5+
请判断一个链表是否为回文链表。
6+
7+
**示例 1:**
8+
```
9+
输入: 1->2
10+
输出: false
11+
```
12+
13+
**示例 2:**
14+
```
15+
输入: 1->2->2->1
16+
输出: true
17+
```
18+
19+
**进阶:**
20+
21+
你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?
22+
23+
### 解法
24+
利用快慢指针,找到链表的中间节点,之后对右部分链表进行逆置,然后左右两部分链表依次遍历,若出现对应元素不相等的情况,返回 `false`;否则返回 `true`
25+
26+
27+
```java
28+
/**
29+
* Definition for singly-linked list.
30+
* public class ListNode {
31+
* int val;
32+
* ListNode next;
33+
* ListNode(int x) { val = x; }
34+
* }
35+
*/
36+
class Solution {
37+
public boolean isPalindrome(ListNode head) {
38+
if (head == null || head.next == null) {
39+
return true;
40+
}
41+
ListNode slow = head;
42+
ListNode fast = head;
43+
while (fast != null && fast.next != null) {
44+
slow = slow.next;
45+
fast = fast.next.next;
46+
}
47+
if (fast != null) {
48+
slow = slow.next;
49+
}
50+
51+
ListNode rightPre = new ListNode(-1);
52+
while (slow != null) {
53+
ListNode t = slow.next;
54+
slow.next = rightPre.next;
55+
rightPre.next = slow;
56+
slow = t;
57+
}
58+
59+
ListNode p1 = rightPre.next;
60+
ListNode p2 = head;
61+
while (p1 != null) {
62+
if (p1.val != p2.val) {
63+
return false;
64+
}
65+
p1 = p1.next;
66+
p2 = p2.next;
67+
}
68+
return true;
69+
70+
}
71+
}
72+
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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;
15+
ListNode fast = head;
16+
while (fast != null && fast.next != null) {
17+
slow = slow.next;
18+
fast = fast.next.next;
19+
}
20+
if (fast != null) {
21+
slow = slow.next;
22+
}
23+
24+
ListNode rightPre = new ListNode(-1);
25+
while (slow != null) {
26+
ListNode t = slow.next;
27+
slow.next = rightPre.next;
28+
rightPre.next = slow;
29+
slow = t;
30+
}
31+
32+
ListNode p1 = rightPre.next;
33+
ListNode p2 = head;
34+
while (p1 != null) {
35+
if (p1.val != p2.val) {
36+
return false;
37+
}
38+
p1 = p1.next;
39+
p2 = p2.next;
40+
}
41+
return true;
42+
43+
}
44+
}

0 commit comments

Comments
 (0)