classSolution:defreverseList(self,head: Optional[ListNode]) -> Optional[ListNode]:# 顶针写法 pre, cur =None, headwhile cur: nxt = cur.next cur.next = pre pre = cur cur = nxtreturn pre
class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
# 法1)
slow, fast = head, head
# 法2)
# slow, fast = head, head.next
while fast and fast.next: # 当 fast 不为 None 时, slow 永远不为 None
slow = slow.next
fast = fast.next.next
return slow