import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
if (head == null || head.next == null) return true;
ListNode p = head;
ArrayList<Integer> list = new ArrayList();
while (p != null) {
list.add(p.val);
p = p.next;
}
ArrayList<Integer> temp = new ArrayList();
temp = (ArrayList<Integer>) list.clone();
//准备一个数组承接翻转之后的数组
Collections.reverse(temp);
for (int i = 0; i < list.size(); i++) {
int x = list.get(i);
int y = temp.get(i);
//正向遍历与反向遍历相同
if (x != y)
return false;
}
return true;
}
}
判断一个链表是否是回环链表
于 2024-05-17 23:32:06 首次发布