TreeviewCopyright © aleen42 all right reserved, powered by aleen42
234. 回文链表
https://leetcode-cn.com/problems/palindrome-linked-list/
Java
class Solution {
public boolean isPalindrome(ListNode head) {
List<Integer> list = new ArrayList<>();
ListNode currentNode = head;
while(currentNode!=null) {
list.add(currentNode.val);
currentNode = currentNode.next;
}
int front = 0,rear = list.size() - 1;
while(rear > front) {
if(!list.get(front).equals(list.get(rear))) {
return false;
}
front++;
rear--;
}
return true;
}
}
Python
'''
@Author: Goog Tech
@Date: 2020-07-22 17:45:49
@Description: https://leetcode-cn.com/problems/palindrome-linked-list/
@FilePath: \leetcode-googtech\#234. Palindrome Linked List\Solution.py
'''
class Solution(object):
def isPalindrome(self, head):
"""
:type head: ListNode
:rtype: bool
"""
values = []
currentNode = head
while currentNode is not None:
values.append(currentNode.val)
currentNode = currentNode.next
return values == values[::-1]