Why is deleting in a single linked list O(1)?

后端 未结 8 2436
悲哀的现实
悲哀的现实 2020-12-25 14:50

I do not quiet understand why deleting at the end of a single linked list goes in O(1) time, as the wikipedia article says.

A single linked list consists out of node

相关标签:
8条回答
  • 2020-12-25 15:05

    If you are give a pointer to the node to be deleted in a single linked list, then you can delete this node in constant time by simply copying the next node onto the node to be deleted.

    M pointer to node to delete
    M.data=M.next.data
    M.next=M.next.next
    

    Otherwise, if you need to search for the node then you cannot do better than O(n)

    0 讨论(0)
  • 2020-12-25 15:07

    For future reference, I must say after some research I found that none of the arguments provided in response to this question are relevant. The answer is that we simply decide for the top of the stack to be the head of the linked list (rather than the tail). This will introduce a slight change into the push routine but then the pop and push will both remain o(1).

    0 讨论(0)
  • 2020-12-25 15:13

    If you're including the cost of fixing the dangling node, you can still do it in O(1) with the use of sentinel node for the end (also described on that page).

    Your "empty" list starts with a single sentinel

    Head -> [Sentinel]
    

    Add some stuff

    Head -> 1 -> 2 -> 3 -> [Sentinel] 
    

    Now delete the tail (3) by marking the node that was 3 as invalid, and then removing the link to the old sentinel, and freeing the memory for it:

    Head -> 1 -> 2 -> 3 -> [Sentinel] 
    Head -> 1 -> 2 -> [Sentinel] -> [Sentinel] 
    Head -> 1 -> 2 -> [Sentinel]
    
    0 讨论(0)
  • 2020-12-25 15:19

    For example, you can have a pointer to the element before last ("second from end") and when deleting: 1. Delete *next of this "second from end" element. 2. Set this "second from end" *next to NULL

    0 讨论(0)
  • 2020-12-25 15:25

    I am not sure I see in the Wikipedia article where it says that it's possible to remove the last entry of a singly-linked list in O(1) time, but that information is incorrect in most cases. Given any individual node in a linked list, it is always possible to remove the node after it in O(1) time by rewiring the list around that new node. Consequently, if you were given a pointer to the penultimate node in a linked list, then you could delete the last element of the list in O(1) time.

    However, if you didn't have any extra pointers into the list other than a head pointer, then you could not delete the last element of the list without scanning to the end of the list, which would require Θ(n) time, as you have noted. You are absolutely correct that just deleting the last node without first changing the pointers into it would be a Very Bad Idea, since if you were to do this the existing list would contain a pointer to a deallocated object.

    More generally - the cost to do an insertion or deletion in a singly-linked list is O(1), assuming you have a pointer to the node right before the one you want to insert or delete. However, you might have to do extra work (up to Θ(n)) to find that node.

    Hope this helps!

    0 讨论(0)
  • 2020-12-25 15:28

    The addition/deletion of ANY node at ANY location is O(1). Code just play with fixed cost (few pointers calculations and malloc/frees) to add/delete the node. This arithmetical cost is fixed for any specific case.

    However, the cost to reach(Indexing) the desired node is O(n).

    The article is merely listing the addition/deletion in multiple sub-categories(adding in middle, beginning, end) to show that cost for adding in middle differs than adding in beginning/end (But the respective costs are still fixed).

    0 讨论(0)
提交回复
热议问题