https://leetcode-cn.com/problems/palindrome-linked-list/
    需要注意的点
    第一种,遍历链表得到数组,复制数组的相反副本,比较数组的值
    第二种,快慢指针

    1. # Definition for singly-linked list.
    2. # class ListNode:
    3. # def __init__(self, val=0, next=None):
    4. # self.val = val
    5. # self.next = next
    6. class Solution:
    7. def isPalindrome(self, head: ListNode) -> bool:
    8. if head is None:
    9. return False
    10. l = []
    11. while head is not None:
    12. l.append(head.val)
    13. head = head.next
    14. l1 = l[::-1]
    15. return l1 == l
    1. # Definition for singly-linked list.
    2. # class ListNode:
    3. # def __init__(self, val=0, next=None):
    4. # self.val = val
    5. # self.next = next
    6. class Solution:
    7. def isPalindrome(self, head: ListNode) -> bool:
    8. slow = head
    9. fast = head
    10. # 推进快慢指针(节点)
    11. # 快节点一次两步,慢节点一次一步
    12. while fast is not None and fast.next is not None:
    13. slow = slow.next
    14. fast = fast.next.next
    15. # 如果快节点不是Null,说明慢节点在中心点
    16. if fast is not None:
    17. slow = slow.next
    18. left = head
    19. right = self.reverseList(slow)
    20. while right is not None:
    21. if left.val != right.val:
    22. return False
    23. left = left.next
    24. right = right.next
    25. return True
    26. '''
    27. 反转链表
    28. '''
    29. def reverseList(self, node: ListNode) -> ListNode:
    30. pre = None
    31. cur = node
    32. while cur is not None:
    33. next = cur.next
    34. cur.next = pre
    35. pre = cur
    36. cur = next
    37. return pre