45
45
# self.val = val
46
46
# self.next = next
47
47
class Solution :
48
- def isPalindrome (self , head : ListNode) -> bool :
49
- if head is None or head.next is None :
50
- return True
48
+ def isPalindrome (self , head : Optional[ListNode]) -> bool :
51
49
slow, fast = head, head.next
52
50
while fast and fast.next:
53
51
slow, fast = slow.next, fast.next.next
@@ -78,9 +76,6 @@ class Solution:
78
76
*/
79
77
class Solution {
80
78
public boolean isPalindrome (ListNode head ) {
81
- if (head == null || head. next == null ) {
82
- return true ;
83
- }
84
79
ListNode slow = head;
85
80
ListNode fast = head. next;
86
81
while (fast != null && fast. next != null ) {
@@ -124,7 +119,6 @@ class Solution {
124
119
class Solution {
125
120
public:
126
121
bool isPalindrome(ListNode* head) {
127
- if (!head || !head->next) return true;
128
122
ListNode* slow = head;
129
123
ListNode* fast = head->next;
130
124
while (fast && fast->next) {
@@ -160,9 +154,6 @@ public:
160
154
* }
161
155
*/
162
156
func isPalindrome(head *ListNode) bool {
163
- if head == nil || head.Next == nil {
164
- return true
165
- }
166
157
slow, fast := head, head.Next
167
158
for fast != nil && fast.Next != nil {
168
159
slow, fast = slow.Next, fast.Next.Next
@@ -200,9 +191,6 @@ func isPalindrome(head *ListNode) bool {
200
191
* @return {boolean}
201
192
*/
202
193
var isPalindrome = function (head ) {
203
- if (! head || ! head .next ) {
204
- return true ;
205
- }
206
194
let slow = head;
207
195
let fast = head .next ;
208
196
while (fast && fast .next ) {
@@ -245,31 +233,23 @@ var isPalindrome = function (head) {
245
233
*/
246
234
public class Solution {
247
235
public bool IsPalindrome (ListNode head ) {
248
- if (head == null || head .next == null )
249
- {
250
- return true ;
251
- }
252
236
ListNode slow = head ;
253
237
ListNode fast = head .next ;
254
- while (fast != null && fast .next != null )
255
- {
238
+ while (fast != null && fast .next != null ) {
256
239
slow = slow .next ;
257
240
fast = fast .next .next ;
258
241
}
259
242
ListNode cur = slow .next ;
260
243
slow .next = null ;
261
244
ListNode pre = null ;
262
- while (cur != null )
263
- {
245
+ while (cur != null ) {
264
246
ListNode t = cur .next ;
265
247
cur .next = pre ;
266
248
pre = cur ;
267
249
cur = t ;
268
250
}
269
- while (pre != null )
270
- {
271
- if (pre .val != head .val )
272
- {
251
+ while (pre != null ) {
252
+ if (pre .val != head .val ) {
273
253
return false ;
274
254
}
275
255
pre = pre .next ;
@@ -296,15 +276,12 @@ public class Solution {
296
276
*/
297
277
298
278
function isPalindrome(head : ListNode | null ): boolean {
299
- if (head == null || head .next == null ) return true ;
300
- // 快慢指针定位到中点
301
279
let slow: ListNode = head ,
302
280
fast: ListNode = head .next ;
303
281
while (fast != null && fast .next != null ) {
304
282
slow = slow .next ;
305
283
fast = fast .next .next ;
306
284
}
307
- // 翻转链表
308
285
let cur: ListNode = slow .next ;
309
286
slow .next = null ;
310
287
let prev: ListNode = null ;
@@ -314,7 +291,6 @@ function isPalindrome(head: ListNode | null): boolean {
314
291
prev = cur ;
315
292
cur = t ;
316
293
}
317
- // 判断回文
318
294
while (prev != null ) {
319
295
if (prev .val != head .val ) return false ;
320
296
prev = prev .next ;
0 commit comments