没做出来 看了解析 但是思路想到了 就是只能调整链表顺序,正确答案是 把链表变成两条单链表,分别从两侧走向中间拿两个指针 分别指向两头 ,往中间走 中途有不一样的就返回false,
java
private static boolean handle(Node head){
int size = size(head);
Node[] reserve = reserve(head, size / 2);
Node left = reserve[0];
Node right = reserve[1];
while(left!=null&&right!=null){
Integer leftvalue=left.getValue();
Integer rightValue=right.getValue();
if(leftvalue!=rightValue){
return false;
}
left=left.next;
right=right.next;
}
return true;
}
其中一个方法是反转链表
java
private static Node[] reserve(Node head,int start){
Node[] result=new Node[2];
Node pre=null;
Node current=head;
int count=0;
while(current!=null){
if(count>start) {
Node next = current.next;
current.next = pre;
pre = current;
current = next;
continue;
}
current=current.next;
count++;
}
result[0]=pre;
result[1]=head;
return result;
}
获取链表长度 方便到时候确定从哪里开始反转
java
private static int size(Node head){
Node current=head;
int count=0;
while(current!=null){
count++;
current=current.next;
}
return count;
}
结束