-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path234.py
More file actions
25 lines (25 loc) · 714 Bytes
/
Copy path234.py
File metadata and controls
25 lines (25 loc) · 714 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
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
if not head or not head.next:
return True
fast, slow = head, head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
temp = head
while temp and temp.next != slow:
temp = temp.next
temp.next = None
curr = slow
prev = None
while curr:
next = curr.next
curr.next = prev
prev = curr
curr = next
while prev and head:
if prev.val != head.val:
return False
prev = prev.next
head = head.next
return True