- 题目:判断一个链表是否为回文
- 难度:Easy
- 思路:
- 代码:
public class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null){
return true;
}
int len = 0;
ListNode node = head;
while(node != null){
len++;
node = node.next;
}
int count = len/2-1;
ListNode pre = null;
ListNode curr = head;
ListNode next = curr.next;
while(count >= 0){
count--;
curr.next = pre;
pre = curr;
curr = next;
next = curr.next;
}
if((len&1) == 1){
curr = next;
}
while(curr != null && pre != null){
if(curr.val != pre.val){
return false;
}
curr = curr.next;
pre = pre.next;
}
return true;
}
}