# 141
class Solution:
def hasCycle(self, head: ListNode) -> bool:
fast , slow = head , head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if fast == slow:
return True
return False
# 142
class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
fast , slow = head , head
while True:
if not (fast and fast.next):return
fast , slow = fast.next.next , slow.next
if fast == slow:
break
fast = head
while fast != slow:
fast , slow = fast.next , slow.next
return fast
# 143
class Solution:
def reorderList(self, head: ListNode) -> None:
"""
Do not return anything, modify head in-place instead.
"""
if not head: return None
p = head
stack = []
while p:
stack.append(p)
p = p.next
n = len(stack)
count = (n - 1) // 2
p = head
while count:
tmp = stack.pop()
tmp.next = p.next
p.next = tmp
p = tmp.next
count -= 1
stack.pop().next = None
# 144
class Solution:
def preorderTraversal(self, root: TreeNode) -> List[int]:
res = []
def helper(root):
if not root:
return
res.append(root.val)
helper(root.left)
helper(root.right)
helper(root)
return res
# 145
class Solution:
def postorderTraversal(self, root: TreeNode) -> List[int]:
res = []
def helper(root):
if not root:
return
helper(root.left)
helper(root.right)
res.append(root.val)
helper(root)
return res
# 146 没看懂呀
# 147
class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
dummy = ListNode(float("-inf"))
pre, tail, cur = dummy, dummy, head
print(cur)
while cur:
if tail.val < cur.val:
tail.next = cur
tail = cur
cur = cur.next
else:
tmp = cur.next
tail.next = tmp
while pre.next and pre.next.val < cur.val:
pre = pre.next
cur.next = pre.next
pre.next = cur
pre = dummy
cur = tmp
return dummy.next
# 148
class Solution:
def sortList(self, head: ListNode) -> ListNode:
if not head and not head.next: return head
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
rpart = slow.next
slow.next = None
lpart = head
left = self.sortList(lpart)
right = self.sortList(rpart)
return self.merge(left, right)
def merge(self, left, right):
dummy = ListNode(0)
p = dummy
l = left
r = right
while l and r:
if l.val < r.val:
p.next = l
l = l.next
p = p.next
else:
p.next = r
r = r. next
p = p.next
if l:
p.next = l
if r:
p.next = r
return dummy.next
# 149
class Solution:
def maxPoints(self, points: List[List[int]]) -> int:
from collections import Counter, defaultdict
points_dict = Counter(tuple(point) for point in points)
not_repeat_point = list(points_dict.keys())
n = len(not_repeat_point)
if n == 1: return points_dict[not_repeat_point[0]]
res = 0
def gcd(x, y):
if y == 0: return x
else: return gcd(y, x%y)
for i in range(n-1):
x1, y1 = not_repeat_point[i][0], not_repeat_point[i][1]
slope = defaultdict(int)
for j in range(i+1, n):
x2, y2 = not_repeat_point[j][0], not_repeat_point[j][1]
dx, dy = x2 - x1, y2 - y1
g = gcd(dy, dx)
if g != 0:
dy //= g
dx //= g
slope["{}/{}".format(dy, dx)] += points_dict[not_repeat_point[j]]
res = max(res, max(slope.values()) + points_dict[not_repeat_point[i]])
return res
# 150
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
stack = []
for i in tokens:
if i in ["+" , '-' , '*' , '/']:
tmp1 = stack.pop()
tmp2 = stack.pop()
stack.append(str(int(eval(tmp2 + i + tmp1))))
else:
stack.append(i)
return int(stack.pop())