本文主要是介绍【hot100篇-python刷题记录】【回文链表】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
R7-链表篇
思路:
转回文数组法
链表转数组,再使用双指针判断是不是回文数组即可。
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=headwhile cur is not None:ret.append(cur.val)cur=cur.nextreturn ret==ret[::-1]
这篇关于【hot100篇-python刷题记录】【回文链表】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!