摘要: def find_middle(self): if not self._head or self._head.next is None: return self._head fast, slow = self._head, self._head fast = fast.next while fast 阅读全文
posted @ 2021-07-12 18:50 水天需 阅读(65) 评论(0) 推荐(0) 编辑
摘要: def merge_list(self, l1, l2): if l1 and l2: p1, p2 = l1, l2 fakeHead = ListNode(None) cur = fakeHead while p1 and p2: if p1.val <= p2.val: cur.next = 阅读全文
posted @ 2021-07-12 18:47 水天需 阅读(141) 评论(0) 推荐(0) 编辑
摘要: # 删除链表倒数第n个节点。假设n大于0 def remove_nth_from_end(head): fast = head count = 0 while fast and count < n: fast = fast._next count += 1 if not fast and count 阅读全文
posted @ 2021-07-12 18:15 水天需 阅读(77) 评论(0) 推荐(0) 编辑
摘要: 为啥一定会相遇呢,可以看这个:为什么用快慢指针找链表的环,快指针和慢指针一定会相遇? 代码: def has_cycle(self): fast, low = self._head, self._head while fast and fast.next: low = low.next fast = 阅读全文
posted @ 2021-07-12 18:01 水天需 阅读(113) 评论(0) 推荐(0) 编辑
摘要: 递归实现 1. 代码 # -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: # 返回ListNode def Revers 阅读全文
posted @ 2021-07-12 16:11 水天需 阅读(233) 评论(0) 推荐(0) 编辑