-
Notifications
You must be signed in to change notification settings - Fork 19
/
answer.py
51 lines (43 loc) · 1.6 KB
/
answer.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
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#-------------------------------------------------------------------------------
# O(n) Time, O(1) Space Complexity (Slightly slower than O(n) space solution)
#-------------------------------------------------------------------------------
class Solution:
def isPalindrome(self, head):
"""
:type head: ListNode
:rtype: bool
"""
# Use fast slow runner to find midpoint
fast = slow = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
prev = None
# Reverse the second half of the list
while slow:
slow.next, prev, slow = prev, slow, slow.next
while head and prev:
if head.val != prev.val:
return False
head = head.next
prev = prev.next
return True
#-------------------------------------------------------------------------------
# Simple Solution with O(n) Space Complexity
#-------------------------------------------------------------------------------
class Solution:
def isPalindrome(self, head):
curr = head
elements = []
while curr:
elements.append(curr.val)
curr = curr.next
return elements == elements[::-1]
#-------------------------------------------------------------------------------