链接:234. 回文链表 - 力扣(LeetCode)

将链表复制到数组中,然后用双指针分别从头尾往中间判断

 1 # Definition for singly-linked list.
 2 # class ListNode(object):
 3 #     def __init__(self, val=0, next=None):
 4 #         self.val = val
 5 #         self.next = next
 6 class Solution(object):
 7     def isPalindrome(self, head):
 8         """
 9         :type head: Optional[ListNode]
10         :rtype: bool
11         """
12         listA = []
13         while head:
14             listA.append(head.val)
15             head = head.next
16         pA = 0
17         num = len(listA)
18         pB = num - 1
19         while pB >= pA:
20             if listA[pA] != listA[pB]:
21                 return False
22             pA += 1
23             pB -= 1
24         return True
25 
26