【hot100篇-python刷题记录】【回文链表】

R7-链表篇

【hot100篇-python刷题记录】【回文链表】_第1张图片

思路: 

转回文数组法

链表转数组,再使用双指针判断是不是回文数组即可。

wkao?!根本不用双指针判断是否回文数组,只需要倒序判断布尔值即可。(牛啊牛啊)

# 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:
        ret=[]
        cur=head
        while cur is not None:
            ret.append(cur.val)
            cur=cur.next
        return ret==ret[::-1]

【hot100篇-python刷题记录】【回文链表】_第2张图片

你可能感兴趣的:(力扣hot100-python,链表,数据结构,python,开发语言,leetcode,算法,职场和发展)