题目描述:
示例:
示例 1: 输入: 1->2 输出: false
示例 2: 输入: 1->2->2->1 输出: true
解答代码:
/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val) { this.val = val; }* ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/class Solution {public boolean isPalindrome(ListNode head) {ArrayList array = new ArrayList<Integer>();if (head == null) {return false;}while(head != null) {array.add(head.val);head = head.next;}int count = array.size();for (int i = 0; i < count; i ++) {if (array.get(i) != array.get(count -i - 1)) {return false;}}return true;}}
解答思路:
运行结果

