25
25
26
26
<!-- 这里可写通用的实现逻辑 -->
27
27
28
+ 先用快慢指针找到链表的中点,接着反转右半部分的链表。然后同时遍历前后两段链表,若前后两段链表节点对应的值不等,说明不是回文链表,否则说明是回文链表。
29
+
28
30
<!-- tabs:start -->
29
31
30
32
### ** Python3**
37
39
# def __init__(self, x):
38
40
# self.val = x
39
41
# self.next = None
40
-
41
42
class Solution :
42
43
def isPalindrome (self , head : ListNode) -> bool :
43
- if not head:
44
+ if head is None or head.next is None :
44
45
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:
69
48
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
72
59
```
73
60
74
61
### ** Java**
@@ -86,43 +73,32 @@ class Solution:
86
73
*/
87
74
class Solution {
88
75
public boolean isPalindrome (ListNode head ) {
89
- if (head == null ) {
76
+ if (head == null || head . next == null ) {
90
77
return true ;
91
78
}
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 ) {
122
82
slow = slow. next;
123
83
fast = fast. next. next;
124
84
}
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 ;
126
102
}
127
103
}
128
104
```
0 commit comments