๐ฏ Learning Goals
- What are doubly linked lists?
- What are its operationsโ time complexities?
๐ง Key Concepts (aka My Notes)
- Two pointers (
nextandprev)- Null
prevmeans it is the head of the list
- Null
Insertion at End
- Same as Singly Linked
- But, make sure to update
prevpointer too - The order is important
- Donโt update the
tailpointer without updating the oldtailโsnextandNewListNodeโsprev)
- Donโt update the
tail.next = NewListNode
NewListNode.prev = tail
# Update tail pointers
tail = tail.next
โ Time complexity isย O(1)
Deletion at End
- Deletion means just not having anything point to that last node
- For this one, important to save the pointer to the node before the
tail- So that, we can set the tail pointer to that node.
Here, ListNode2 is just a variable name. It can be temp or anything.
ListNode2 = tail.prev
ListNode2.next = null
tail = ListNode2
โ Time complexity isย O(1)
O(1) insertion and deletion means that theoretically we can use these guys are implement Stacks.
Access
- Same as Singly Linked List, but can go both directions
โ Time complexity isย O(n)
โ Time Complexities
| Operation | Big-O |
|---|---|
| Access | O(n) |
| Search | O(n) |
| Insertion (end) | O(1) |
| Deletion (end) | O(1) |
Top comments (0)