1
class Solution:
2
def isPalindrome(self, head: "Optional[ListNode]") -> bool:
3
if head.next == None:
4
return True # if only 1 element, it's always a palindrome
5
forward = head
6
first_half = []
7
fast = head
8

9
while fast != None and fast.next != None:
10
first_half.append(forward.val)
11
forward = forward.next
12
fast = fast.next.next
13

14
# forward should now be through half the list
15
if fast != None:
16
forward = forward.next # if length isn't even, skip the middle number
17

18
reverse = len(first_half) - 1
19
while forward != None:
20
if forward.val != first_half[reverse]:
21
return False
22
forward = forward.next
23
reverse -= 1
24

25
return True

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0