思路:
- 复制链表值到数组列表中。
- 使用双指针法判断是否为回文。
代码:
# Definition for singly-linked list.
from typing import Optional
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def isPalindrome(self, head: Optional[ListNode]) -> bool:
tmp = []
curr = head
while curr:
tmp.append(curr.val)
curr = curr.next
return tmp == tmp[::-1]