Q:

请判断一个链表是否为回文链表。

示例 1:

输入: 1->2
输出: false

示例 2:

输入: 1->2->2->1
输出: true

链接:https://leetcode-cn.com/problems/palindrome-linked-list/description/

思路:遍历链表,判断遍历结果是否是回文串

代码:

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def isPalindrome(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        if not head:
            return True
        tmp = []
        pre = head
        while pre.next:
            tmp.append(pre.val)
            pre = pre.next
        tmp.append(pre.val)
        return tmp == tmp[::-1]
        

Leetcode_总结】234. 回文链表 - python

相关文章:

  • 2021-07-21
  • 2021-10-04
  • 2021-07-05
  • 2021-09-20
  • 2022-12-23
  • 2021-04-08
  • 2021-07-05
  • 2021-10-07
猜你喜欢
  • 2021-10-19
  • 2021-08-04
  • 2022-02-16
  • 2022-01-22
  • 2022-12-23
相关资源
相似解决方案