import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
// 快慢指针分成两段
ListNode slow = head;
ListNode fast = head;
while (fast != null) {
slow = slow.next;
fast = fast.next;
if (fast != null) {
fast = fast.next;
}
}
// 反转后半段
ListNode pre = null;
ListNode current = slow;
while (current != null) {
ListNode next = current.next;
current.next = pre;
pre = current;
current = next;
}
// 比对两个链表
ListNode pointer1 = pre;
ListNode pointer2 = head;
while (pointer1 != null) {
if (pointer1.val != pointer2.val) {
return false;
}
pointer1 = pointer1.next;
pointer2 = pointer2.next;
}
return true;
}
}