题解 | #判断一个链表是否为回文结构#
判断一个链表是否为回文结构
https://www.nowcoder.com/practice/3fed228444e740c8be66232ce8b87c2f
解题思路
- 先遍历链表,将元素保存在数组内;
- 利用数组的性质,使用切片的方法,对数组进行比较,得到返回结果。
- 时间复杂度和空间复杂度都为O(n)。
class Solution: def isPail(self , head: ListNode) -> bool: # write code here p = head a = [] while p: a.append(p.val) p = p.next if a == a[::-1]: return True else: return False