-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path227.py
More file actions
33 lines (28 loc) · 865 Bytes
/
227.py
File metadata and controls
33 lines (28 loc) · 865 Bytes
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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def isPalindrome(self, head: Optional[ListNode]) -> bool:
if head is None or head.next is None:
return True
slow = head
fast = head
while (fast and fast.next) is not None:
slow = slow.next
fast = fast.next.next
prev = None
while slow is not None:
next_node = slow.next
slow.next = prev
prev = slow
slow = next_node
first = prev
second = head
while (first and second) is not None:
if first.val != second.val:
return False
first = first.next
second = second.next
return True