标签:for OLE == while tno class bool false null
请判断一个链表是否为回文链表。
示例 1:
输入: 1->2 输出: false
示例 2:
输入: 1->2->2->1 输出: true
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public boolean isPalindrome(ListNode head) { List<ListNode> list = new ArrayList<>(); if(head == null) return true; while(head != null) { list.add(head); head = head.next; } return PalindList(list); } public boolean PalindList(List<ListNode> list) { int n = list.size(); for(int i = 0; i < list.size() / 2; i++) { if(list.get(i).val != list.get(n - i - 1).val) { return false; } } return true; } }
标签:for OLE == while tno class bool false null
原文地址:https://www.cnblogs.com/Duancf/p/12632984.html