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