# ＜LeetCode天梯＞Day028 回文链表（双指针+递归+栈+数组） | 初级算法 | Python

?作者简介：大家好，我是车神哥，府学路18号的车神?

?个人主页：应无所住而生其心的博客_府学路18号车神_CSDN博客
?点赞评论收藏 == 养成习惯（一键三连）?
?本系列主要以刷LeetCode力扣）网站的各类题为标准，实现自我能力的提升为目标⚡
⚡希望大家多多支持?~一起加油 ?

# 题干

• 链表中节点数目在范围[1, 105] 内
• 0 <= Node.val <= 9

## 双指针（反转后半部分链表）

``````class Solution:
def isPalindrome(self, head: ListNode) -> bool:
# 双指针

return True

# 若fast不为空值，则链表的长度为奇数
# if fast != None:
#     slow = slow.next

# 反转后半部分链表
out = None
return out

# 通过快慢指针找到中点
while fast  and fast.next:
fast = fast.next.next
slow = slow.next

slow = reversenode(slow)
# 依次比较节点值是否相同
return False
# else:
slow = slow.next
return True
``````

## 递归法

``````def reverseListNode(head):
return  # 终止条件
pre = None
return pre
``````

``````class Solution:
def isPalindrome(self, head: ListNode) -> bool:
# 递归

if current_node is not None:
if not re_check(current_node.next):
return False
if self.front_pointer.val != current_node.val: # 比较
return False
self.front_pointer = self.front_pointer.next
return True

return re_check()
``````

## 栈

``````class Solution:
def isPalindrome(self, head: ListNode) -> bool:
stack = []
# Push 操作
while current_node:
stack.append(current_node)
current_node = current_node.next
# POP + Compare 删除和比较
while stack:
node2 = stack.pop()  # 删除最后一个，将其删除值赋值给node2
if node.val != node2.val:
return False
node = node.next
return True
``````

## 链表转数组比对

``````class Solution:
def isPalindrome(self, head: ListNode) -> bool:
# 数组
sav = []             # 设置空list
return sav == sav[::-1]

``````

# Reference

## 加油！！！

❤坚持读Paper，坚持做笔记，坚持学习，坚持刷力扣LeetCode❤！！！

To Be No.1

⚡⚡

ღ( ´･ᴗ･` )

THE END