-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSolution234.java
More file actions
33 lines (29 loc) · 893 Bytes
/
Solution234.java
File metadata and controls
33 lines (29 loc) · 893 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package LinkedList_problem;
/**
* (1、遍历存到数组、判断数组。2、双指针)
* 回文链表: 快慢指针(整除器),把剩下的一半变成逆序,再进行比较。注意奇偶情况讨论。
*/
class Solution234 {
public boolean isPalindrome(ListNode head) {
if(head==null) return true;
ListNode prev=null;
ListNode slow=head, fast=head;
while(fast!=null&&fast.next!=null){
slow=slow.next;
fast=fast.next.next;
}
if (fast!=null) slow=slow.next;
while(slow!=null){
ListNode tmp=slow;
slow=slow.next;
tmp.next=prev;
prev=tmp;
}
slow=prev;fast=head;
while(slow!=null){
if(slow.val==fast.val){slow=slow.next; fast=fast.next;}
else return false;
}
return true;
}
}