-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path234.py
53 lines (51 loc) · 1.62 KB
/
234.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
__________________________________________________________________________________________________
sample 60 ms submission
# 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
slow = head
fast = head
stack = [slow.val]
while fast and fast.next:
slow = slow.next
fast = fast.next.next
stack.append(slow.val)
stack.pop()
if fast:
slow = slow.next
while slow:
if slow.val != stack.pop():
return False
slow = slow.next
return True
__________________________________________________________________________________________________
sample 23464 kb submission
# 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 = slow = head
while fast.next != None and fast.next.next != None:
fast = fast.next.next
slow = slow.next
if fast != None:
slow = slow.next
half = self.reverse(slow)
while half != None:
if head.val != half.val:
return False
head = head.next
half = half.next
return True
__________________________________________________________________________________________________