NC96 判断一个链表是否为回文结构
判断一个链表是否为回文结构_牛客题霸_牛客网 (nowcoder.com)
import java.util.*;
public class Solution {
public ListNode reverse(ListNode head){
ListNode prev=null;
ListNode cur=head;
while(cur!=null){
ListNode next=cur.next;
cur.next=prev;
prev=cur;
cur=next;
}
return prev;
}
public boolean isPail (ListNode head) {
if(head==null){
return true;
}
ListNode slow=head;
ListNode fast=head;
while(fast!=null&&fast.next!=null){
slow=slow.next;
fast=fast.next.next;
}
slow=reverse(slow);
while(slow!=null){
if(slow.val!=head.val){
return false;
}
slow=slow.next;
head=head.next;
}
return true;
}
}