给你一个单链表的头节点 head
,请你判断该链表是否为回文链表。如果是,返回 true
;否则,返回 false
。
示例 1:
输入:head = [1,2,2,1] 输出:true
# 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=[]#定义空列表while head is not None:#判断是否为空vals.append(head.val)#向列表最后中插入元素,类似push_backhead=head.nextreturn vals==vals[::-1]