题目链接 | 2095. 删除链表的中间节点 |
---|---|
思路 | 快慢指针-找到中间节点-简单扩展 |
题解链接 | 官方题解 |
关键点 | while fast and fast.next: ... |
时间复杂度 | \(O(n)\) |
空间复杂度 | \(O(1)\) |
代码实现:
class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head.next is None:
return None
slow, fast, prev = head, head, None
while fast and fast.next:
fast = fast.next.next
prev, slow = slow, slow.next
prev.next = slow.next
return head
标签:2095,head,slow,fast,next,链表,节点
From: https://www.cnblogs.com/WrRan/p/18409093