一、题目
二、代码
/**
* 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
{
//array list
public boolean isPalindrome(ListNode head)
{
List<Integer> vals = new ArrayList<Integer>();
ListNode cur =head;
while(cur!=null)
{
vals.add(cur.val);
cur = cur.next;
}
int before = 0;
int last = vals.size()-1;
// System.out.println(" before "+before);
// System.out.println(" last "+last);
while(before<last)
{
if(vals.get(before)!=vals.get(last)) return false;
before++;
last--;
}
return true;
}
}