linkedLists Flashcards
What’s the time complexity for traversing arrays?
Time: 0(1), because arrays occupy contiguous (adjacent) blocks of memory
What’s the time complexity for traversing linked lists?
Time: 0(n), because linked list nodes can exist anywhere in memory
What’s the time and space complexity for linked lists?
Time: 0(n), because linked list nodes can exist anywhere in memory
Space: 0(n), because it requires n nodes to store n values. Linked lists hold two main pieces of information (the value and pointer) per node. This means that the amount of data stored increases linearly with the number of nodes in the list.
Whats the time and space complexity for doubly linked lists?
Time: 0(1), because of the two addresses in a node (next and previous), this makes it efficient.
space: 0(n), because it holds two addresses in a node, one is of the next node and the other one is of the previous node, this takes more memory
What’s the time complexity for addToHead()?
Time: 0(1), addToHead() is O(1) since each of its steps operate in constant time, regardless of the size of the list
Because linked lists have a time complexity of 0(n) and addToHead() has a time complexity of 0(1), to add to head on a linked list would take 0(n) time.
What’s the time complexity for print() or search()?
Time: 0(n), because it requires visiting n nodes
What’s the time complexity for addToTail()?
Time: 0(n), because it requires visiting n nodes
What’s the time complexity for removeFromHead() and removeFromTail() for linked lists?
Time: 0(n)