Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is removing a node from a doubly-linked list faster than removing a node from a singly-linked list?

I was curious why deleting a node from a double linked list is faster than a single linked. According to my lecture, it takes O(1) for a double linked list compared to O(n) for a single linked. According to my thought process, I thought they both should be O(n) since you have to traverse across possibly all the elements so it depends on the size.

I understand it's going be associated with the fact that each node has a previous pointer and a next pointer to the next node, I just can't understand how it would be a constant operation in the sense of O(1)

like image 590
James Wilks Avatar asked Oct 08 '13 02:10

James Wilks


People also ask

Which is faster singly linked list or doubly linked list?

Accessing elements in a Singly Linked List is less efficient when compared to a Doubly Linked List, as only forward traversal is possible. Accessing elements in a Doubly Linked List is more efficient when compared to a Singly Linked List as both forward and backward traversal is possible.

What's the time complexity of removing an element from a doubly linked list?

In LRU cache design, deletion in doubly linked list takes O(1) time.

Why do you think that a doubly linked list is better than a single linked list justify with the help of examples?

If we need better performance while searching and memory is not a limitation in this case doubly linked list is more preferred. As singly linked list store pointer of only one node so consumes lesser memory. On other hand Doubly linked list uses more memory per node(two pointers).


2 Answers

This partially depends on how you're interpreting the setup. Here are two different versions.

Version 1: Let's suppose that you want to delete a linked list node containing a specific value x from a singly or doubly-linked list, but you don't know where in the list it is. In that case, you would have to traverse the list, starting at the beginning, until you found the node to remove. In both a singly- and doubly-linked list, you can then remove it in O(1) time, so the overall runtime is O(n). That said, it's harder to do the remove step in the singly-linked list, since you need to update a pointer in the preceding cell (which isn't pointed at by the cell to remove), so you need to store two pointers as you do this.

Version 2: Now let's suppose you're given a pointer to the cell to remove and need to remove it. In a doubly-linked list, you can do this by using the next and previous pointers to identify the two cells around the cell to remove and then rewiring them to splice the cell out of the list. This takes time O(1). But what about a singly-linked list? To remove this cell from the list, you have to change the next pointer of the cell that appears before the cell to remove so that it no longer points to the cell to remove. Unfortunately, you don't have a pointer to that cell, since the list is only singly-linked. Therefore, you have to start at the beginning of the list, walk downwards across the nodes, and find the node that comes right before the one to remove. This takes time O(n), so the runtime for the remove step is O(n) in the worst case, rather than O(1). (That said, if you know two pointers - the cell you want to delete and the cell right before it, then you can remove the cell in O(1) time since you don't have to scan the list to find the preceding cell.)

In short: if you know the cell to remove in advance, the doubly-linked list lets you remove it in time O(1) while a singly-linked list would require time O(n). If you don't know the cell in advance, then it's O(n) in both cases.

Hope this helps!

like image 198
templatetypedef Avatar answered Sep 28 '22 00:09

templatetypedef


The list does not have to be traversed in order to connect the previous node to the following node in a double-linked list. You simply point

curr.Prev.Next = curr.Next and curr.Next.Prev = curr.Prev.

In a single-linked list, you have to traverse the list to find the previous node. Traversal can be O(n) in a non-sorted list.

like image 27
Keith Payne Avatar answered Sep 27 '22 22:09

Keith Payne