https://leetcode-cn.com/problems/palindrome-linked-list/
思路
- 链表元素值存入 list
- 双指针 (0 开始, 末尾开始) 判断值是否相等
/**
链表值加入集合
双指针
*/
public boolean isPalindrome(ListNode head) {
List<Integer> list = new ArrayList<>();
ListNode curr = head;
while (curr != null) {
list.add(curr.val);
curr = curr.next;
}
int first = 0;
int last = list.size() - 1;
while (first < last) {
if (list.get(first) != list.get(last)) {
return false;
}
first++;
last--;
}
return true;
}