234. Palindrome Linked List
Example 1:

Input: head = [1,2,2,1] Output: true
Example 2:

Input: head = [1,2] Output: false
public boolean isPalindrome(ListNode head) {
ListNode mid = getMid(head);
ListNode headSecond = reverseList(mid);
while(head != null && headSecond != null){
if(head.val != headSecond.val){
return false;
}
head = head.next;
headSecond = headSecond.next;
}
return true;
}
public ListNode getMid(ListNode head){
ListNode slow = head;
ListNode fast = head;
while(fast!= null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
public ListNode reverseList(ListNode node){
// 反转链表部分可参考 反转链表
}
浙公网安备 33010602011771号