回文链表
请判断一个链表是否为回文链表。
示例 1:
输入: 1->2 输出: false
示例 2:
输入: 1->2->2->1 输出: true
进阶:
你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?
class Solution {
public static boolean isPalindrome(ListNode head) {
if(head == null){
return true;
}
boolean flag = true;
ListNode pre = null ;
ListNode next = null;
ListNode hishead = new ListNode(head.val);
ListNode hh = hishead;
while(head != null){
hishead.next = new ListNode(head.val);
hishead = hishead.next;
next = head.next;
head.next = pre;
pre = head;
head = next;
}
hh = hh.next;
while(hh != null){
if(hh.val != pre.val){
return false;
}
hh = hh.next;
pre = pre.next;
}
return flag;
}
}