forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
palindrome-linked-list.py
38 lines (34 loc) · 1.13 KB
/
palindrome-linked-list.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
# Time: O(n)
# Space: O(1)
#
# Given a singly linked list, determine if it is a palindrome.
#
# Follow up:
# Could you do it in O(n) time and O(1) space?
#
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
class Solution:
# @param {ListNode} head
# @return {boolean}
def isPalindrome(self, head):
reverse, fast = None, head
# Reverse the first half part of the list.
while fast and fast.next:
fast = fast.next.next
head.next, reverse, head = reverse, head, head.next
# If the number of the nodes is odd,
# set the head of the tail list to the next of the median node.
tail = head.next if fast else head
# Compare the reversed first half list with the second half list.
# And restore the reversed first half list.
is_palindrome = True
while reverse:
is_palindrome = is_palindrome and reverse.val == tail.val
reverse.next, head, reverse = head, reverse, reverse.next
tail = tail.next
return is_palindrome