-
Notifications
You must be signed in to change notification settings - Fork 0
/
letcode_234_回文链表.py
92 lines (77 loc) · 1.85 KB
/
letcode_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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- encoding: utf-8 -*-
"""
@File : letcode_234_回文链表.py
@Time : 2020/10/23 上午8:39
@Author : dididididi
@Email :
@Software: PyCharm
"""
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solutions(object):
def isPalindrome(self, head):
"""
:type head: ListNode
:rtype: bool
"""
vals = []
while head:
vals.append(head.val)
head = head.next
return vals == vals[::-1]
class Solution(object):
"""
快慢指针,找中点翻转列表
"""
def isPalindrome(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if not head:
return True
mid = self.middleNode(head)
l1 = head
l2 = mid.next
mid.next = None
l2 = self.reverseList(l2)
while l2:
if l1.val != l2.val:
return False
l1 = l1.next
l2 = l2.next
return True
def middleNode(self, head: ListNode) -> ListNode:
"""
查找中点
:param head:
:return:
"""
slow = fast = head
while fast.next and fast.next.next:
fast = fast.next.next
slow = slow.next
return slow
def reverseList(self, head: ListNode) -> ListNode:
"""
翻转列表
:param head:
:return:
"""
prev = None
while head:
temp = head.next
head.next = prev
prev = head
head = temp
return prev
if __name__ == '__main__':
head = ListNode(1)
head.next = ListNode(2)
head.next.next = ListNode(2)
head.next.next.next = ListNode(2)
head.next.next.next.next = ListNode(1)
sol = Solution()
sol.isPalindrome(head)