-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution.py
67 lines (58 loc) · 1.56 KB
/
solution.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# Created by Bruce yuan on 18-1-31.
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def isPalindrome(self, head):
"""
这题我想不到怎么 O(1) space
:type head: ListNode
:rtype: bool
"""
current = head
count = 0
while current:
count += 1
current = current.next
if count == 1:
return True
# 把她重新用作中位数
count = count // 2 if count % 2 == 0 else count // 2 + 1
current = head
for _ in range(count):
current = current.next
# 123 4 321 0 0
current = self.reverse_list(current)
# print(current.val)
# print(head.val)
while current and head:
# print('test test')
if current.val != head.val:
return False
current = current.next
head = head.next
# print(current)
# print(head)
return True
@staticmethod
def reverse_list(head):
pre = None
# 这就是头插法,竟然都忘记了。简直了
while head:
next_ = head.next
head.next = pre
pre = head
head = next_
return pre
def main():
head = ListNode(0)
next_ = ListNode(0)
head.next = next_
solution = Solution()
result = solution.isPalindrome(head)
print(result)
if __name__ == '__main__':
main()