链表
对于实际操作,链表的插入时间复杂度为O(1),但需要O(n)时间遍历到正确的位置。大多数在线资源将链接列表的平均插入时间列为O(1):
https://stackoverflow.com/a/17410009/10426919
https://www.bigocheatsheet.com/
https://www.geeksforgeeks.org/time-complexities-of-different-data-structures/
BST
二进制搜索树的插入需要遍历节点,所需时间为O(log )。
问题
Am I mistaken to believe that insert
尝试学习codesignal的链表教程,但在理解以下代码时遇到困难。我知道每一行都做了什么,但不知道为什么我们将l赋值给tmp,并在最后检查l。
# Definition for singly-linked list:
# class ListNode(object):
# def __init__(self, x):
# self.value = x
# self.next = None
#
def removeKFromList(l, k):
tmp = l
while tmp:
if tmp.next and tmp.next.valu