题解 | #判断一个链表是否为回文结构#
判断一个链表是否为回文结构
https://www.nowcoder.com/practice/3fed228444e740c8be66232ce8b87c2f
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
public boolean isPail (ListNode head) {
ArrayList<Integer> arr=new ArrayList<>();
while(head!=null){
arr.add(head.val);
head=head.next;
}
int left=0;
int right=arr.size()-1;
while(left<=right){
int n1=arr.get(left);
int n2=arr.get(right);
if(n1!=n2){
return false;
}
left++;
right--;
}
return true;
}
}
查看21道真题和解析