LeetCode刷题笔记第234题:回文链表
想法:
要判断链表是否是回文,先将链表的值取出放入列表中,再判断列表与其倒叙是否一致,以此来判断是否回文。
# 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: ListNode) -> bool:
# 创建列表存放链表中的值
vals = []
current_node = head
# 遍历整个链表将链表中的元素存放在列表中
while current_node is not None:
vals.append(current_node.val)
current_node = current_node.next
# 返回是否为回文的判断
return vals == vals[::-1]