回文链表是指从头遍历与从后遍历得到的结点顺序一致
解决方法:找到链表的中间结点,将链表的后半边结点进行逆置,然后比较
public class PalindromeList {
public ListNode getMid(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while (fast != null) {
fast = fast.next;
if (fast == null) {
break;
}
fast = fast.next;
slow = slow.next;
}
return slow;
}//利用快慢指针求中间结点
public ListNode reverse(ListNode head) {
ListNode result = null;
ListNode cur = head;
while (cur != null) {
ListNode next = cur.next;
cur.next = result;
result = cur;
cur = next;
}
return result;
}//链表逆置
public boolean chkPalindrome(ListNode A) {
ListNode mid = getMid(A);
ListNode h2 = reverse(mid);
ListNode n1 = A;
ListNode n2 = h2;
while (n1 != null && n2 != null) {
if (n1.val != n2.val) {
return false;
}
n1 = n1.next;
n2 = n2.next;
}
return true;
}
}