public class Node { public int data; public Node next; public Node() { next = null; } } public class List { private Node head; public List() { head = new Node(); } public boolean isSymmetrical() { int[] arr = new int[100]; int count = 0; Node current = head.next; while (current != null) { arr[count++] = current.data; current = current.next; } for (int i = 0; i < count/2; i++) { if (arr[i] != arr[count-i-1]) { return false; } } return true; } } 该方法的时间复杂度为O(n),其中n为链表的长度。


原文地址: https://www.cveoy.top/t/topic/pF66 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录