File tree Expand file tree Collapse file tree 3 files changed +122
-1
lines changed
src/com/andavid/leetcode/_234 Expand file tree Collapse file tree 3 files changed +122
-1
lines changed Original file line number Diff line number Diff line change 49
49
| 19 | [ Remove Nth Node From End of List] [ 019 ] |
50
50
| 206 | [ Reverse Linked List] [ 206 ] |
51
51
| 21 | [ Merge Two Sorted Lists] [ 021 ] |
52
-
52
+ | 234 | [ Palindrome Linked List ] [ 234 ] |
53
53
54
54
55
55
** 其他**
89
89
[ 019 ] : https://github.com/andavid/leetcode-java/blob/master/note/019/README.md
90
90
[ 206 ] : https://github.com/andavid/leetcode-java/blob/master/note/206/README.md
91
91
[ 021 ] : https://github.com/andavid/leetcode-java/blob/master/note/021/README.md
92
+ [ 234 ] : https://github.com/andavid/leetcode-java/blob/master/note/234/README.md
Original file line number Diff line number Diff line change
1
+ # [ Palindrome Linked List] [ title ]
2
+
3
+ ## Description
4
+
5
+ Given a singly linked list, determine if it is a palindrome.
6
+
7
+ ** Follow up:**
8
+ Could you do it in O(n) time and O(1) space?
9
+
10
+
11
+ ## 思路
12
+
13
+ 使用快慢两个指针找到链表中点,慢指针每次前进一步,快指针每次前进两步。在慢指针前进的过程中,同时修改其 next 指针,使得链表前半部分反序。最后比较中点两侧的链表是否相等。
14
+
15
+ ## [ 完整代码] [ src ]
16
+
17
+ ``` java
18
+ /**
19
+ * Definition for singly-linked list.
20
+ * public class ListNode {
21
+ * int val;
22
+ * ListNode next;
23
+ * ListNode(int x) { val = x; }
24
+ * }
25
+ */
26
+ class Solution {
27
+ public boolean isPalindrome (ListNode head ) {
28
+ if (head == null || head. next == null ) {
29
+ return true ;
30
+ }
31
+
32
+ ListNode prev = null ;
33
+ ListNode slow = head;
34
+ ListNode fast = head;
35
+
36
+ while (fast != null && fast. next != null ) {
37
+ fast = fast. next. next;
38
+ ListNode next = slow. next;
39
+ slow. next = prev;
40
+ prev = slow;
41
+ slow = next;
42
+ }
43
+
44
+ if (fast != null ) {
45
+ slow = slow. next;
46
+ }
47
+
48
+ while (slow != null ) {
49
+ if (slow. val != prev. val) {
50
+ return false ;
51
+ }
52
+ slow = slow. next;
53
+ prev = prev. next;
54
+ }
55
+
56
+ return true ;
57
+ }
58
+ }
59
+ ```
60
+
61
+ [ title ] : https://leetcode.com/problems/palindrome-linked-list
62
+ [ src ] : https://github.com/andavid/leetcode-java/blob/master/src/com/andavid/leetcode/_234/Solution.java
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+
3
+ public static boolean isPalindrome (ListNode head ) {
4
+ if (head == null || head .next == null ) {
5
+ return true ;
6
+ }
7
+
8
+ ListNode prev = null ;
9
+ ListNode slow = head ;
10
+ ListNode fast = head ;
11
+
12
+ while (fast != null && fast .next != null ) {
13
+ fast = fast .next .next ;
14
+ ListNode next = slow .next ;
15
+ slow .next = prev ;
16
+ prev = slow ;
17
+ slow = next ;
18
+ }
19
+
20
+ if (fast != null ) {
21
+ slow = slow .next ;
22
+ }
23
+
24
+ while (slow != null ) {
25
+ if (slow .val != prev .val ) {
26
+ return false ;
27
+ }
28
+ slow = slow .next ;
29
+ prev = prev .next ;
30
+ }
31
+
32
+ return true ;
33
+ }
34
+
35
+ public static class ListNode {
36
+ int val ;
37
+ ListNode next ;
38
+ ListNode (int x ) {
39
+ val = x ;
40
+ }
41
+ }
42
+
43
+ public static void main (String [] args ) {
44
+ ListNode node1 = new ListNode (1 );
45
+ ListNode node2 = new ListNode (2 );
46
+ ListNode node3 = new ListNode (3 );
47
+ ListNode node4 = new ListNode (2 );
48
+ ListNode node5 = new ListNode (1 );
49
+ node1 .next = node2 ;
50
+ node2 .next = node3 ;
51
+ node3 .next = node4 ;
52
+ node4 .next = node5 ;
53
+ node5 .next = null ;
54
+
55
+ System .out .println (isPalindrome (node1 ));
56
+ }
57
+
58
+ }
You can’t perform that action at this time.
0 commit comments