# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
if not head :
return True
fast=head
slow=head
while(fast.next and fast.next.next):
fast=fast.next.next
slow=slow.next
slow=slow.next
if not slow:
return True
newhead=ListNode(slow.val)
while(slow.next):
slow=slow.next
cur=ListNode(slow.val)
cur.next=newhead
newhead=cur
while(newhead):
if newhead.val!=head.val:
return False
head=head.next
newhead=newhead.next
return True
本文介绍了一种使用Python实现的单链表回文判断算法。通过快慢指针找到链表中点,反转后半部分链表并与前半部分进行比较,以此判断链表是否为回文结构。
1006

被折叠的 条评论
为什么被折叠?



