# 回文链表

给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false

 

示例 1:

输入:head = [1,2,2,1]
输出:true

示例 2:

输入:head = [1,2]
输出:false

 

提示:

 

进阶:你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?

## template ```java public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } class Solution { public boolean isPalindrome(ListNode head) { if (head == null || head.next == null) { return true; } ListNode slow = head; ListNode quick = head; while (quick != null && quick.next != null) { quick = quick.next.next; slow = slow.next; } ListNode pre = null; ListNode p = slow; while (p != null) { ListNode temp = p.next; p.next = pre; pre = p; p = temp; } while (pre != null) { if (pre.val == head.val) { pre = pre.next; head = head.next; } else { return false; } } return true; } } ``` ## 答案 ```java ``` ## 选项 ### A ```java ``` ### B ```java ``` ### C ```java ```