编写一个函数,检查输入的链表是否是回文的。
示例 1:
输入: 1->2
输出: false
示例 2:
输入: 1->2->2->1
输出: true
解法1:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
l = []
while head:
l.append(head.val)
head = head.next
return l == l[::-1]
【leetcode-python】面试题 02.06. 回文链表
最新推荐文章于 2022-02-02 22:17:24 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)