Problem Solving Ideas:
The spatial complexity of O (1) means that the problem cannot be solved by opening a list. We can divide the list into front and back two parts, and the second half is flipped by the reciprocal assignment of pointers.
The Java implementation is as follows:
public static Boolean Ispalindrome (ListNode head) {if (head = = NULL | | head.next = = NULL) return true; ListNode middle = head, fast = Head;while (FAST! = NULL && Fast.next! = null) {middle = Middle.next;fast = Fast.ne Xt.next;} ListNode righthalf = Reverselistnode (middle), while (righthalf! = null) {if (righthalf.val! = head.val) Return false;head = Head.next;righthalf = Righthalf.next;} return true;} public static ListNode Reverselistnode (ListNode head) {if (head = = NULL | | head.next = = NULL) return head; ListNode pre = head, cur = head.next, tmp = Head;head.next = Null;while (cur! = null) {tmp = Cur.next;cur.next = Pre;pre = Cur;cur = tmp;} return pre;}
Java for Leetcode 234 palindrome Linked List