算法-回文链表
请判断一个链表是否为回文链表。
示例 1:
输入: 1->2
输出: false
示例 2:
输入: 1->2->2->1
输出: true
来源:力扣(LeetCode)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null){return true;}
List<Integer> ls = new ArrayList<Integer>();
while(head != null){
ls.add(head.val);
head = head.next;
}
boolean result = false;
for(int i = 0; i < ls.size(); i++){
if(ls.get(i).equals(ls.get(ls.size()-1-i))){result = true;}
else{return false;}
}
return result;
}
}