Time Complexity in singly link list

前端 未结 3 2026
栀梦
栀梦 2021-02-14 14:13

I am studying data-structure: singly link list.

The website says singly linked list has a insertion and deletion time complexity of O(1). Am I missing some

3条回答
  •  遇见更好的自我
    2021-02-14 14:43

    The explanation for this is, that the big O notation in the linked table refers to the function implementation itself, not including the list traversal to find the previous reference node in the list.

    If you follow the link to the wikipedia article of the Singly-LinkedList implementation it becomes more clear:

    function insertAfter(Node node, Node newNode)
    function removeAfter(Node node)     
    

    The above function signatures already take the predecessor node as argument (same for the other variants implicitly).

    Finding the predecessor is a different operation and may be O(n) or other time complexity.

提交回复
热议问题