代码思路:将链表的值复制到数组列表中,再使用双指针法判断,不断更新current_node的值。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def isPalindrome(self, head: Optional[ListNode]) -> bool:
vals=[]
current_node = head #不断循环更新current_node的值
while current_node is not None:
vals.append(current_node.val)
current_node = current_node.next
return vals == vals[::-1]