-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path234.py
42 lines (35 loc) · 1020 Bytes
/
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
import re
import collections
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
# Using deque
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
d = collections.deque()
curr_node = head
while curr_node:
d.append(curr_node.val)
if(curr_node.next):
curr_node = curr_node.next
else:
break
while len(d) > 1:
if d.popleft() != d.pop():
return False
return True
# Elegant
class Solution2:
def isPalindrome(self, head: ListNode) -> bool:
rev = None
slow = fast = head
while fast and fast.next:
fast = fast.next.next
rev, rev.next, slow = slow, rev, slow.next
if fast:
slow = slow.next
while rev and rev.val == slow.val:
slow, rev = slow.next, rev.next
return not rev