If we want to add in a new node, we just need to modify the pointer of the previous node & the pointer of the next node - in constant time. Since the connection between the 2 nodes are via Pointer, we can have nodes all around the Main Memory. This means we can add more nodes as long as there is free memory size that can fit a single node.
Cache Miss!!!
Since the connection between 2 nodes are via Pointer, the nodes are scattered around the Main Memory. This means we can’t make use of Cache Locality and this results in Cache Miss.
public class Node { int val; Node next; Node(){} Node(int val) this.val = val;}
Print Out Linked List
public void printLinkedList(ListNode node) { ListNode a = node; while (node!=null) { System.out.printf("%d ", node.val); node = node.next; } System.out.println();}
Draw It Out
When unsure about the relationship, draw out the nodes & pointers. It really makes the visualization of the manipulation easy!
Virtual Node
O(1) to access either the head node or the tail node, this makes handling edge cases & reverting linked list easier.
Time Complexity
Search
O(n) to search for a value.
Indexing
It takes O(n) to obtain the node at a particular position, because the desired Memory Address can’t be obtained by simply incrementing the index like what we can do with Array. what we have at the start of every indexing is the memory address of the next node, we need to traverse n-1 nodes in order to access nth node.
Insert, Delete
O(1), we only need the node’s previous node and next node, make the corresponding pointer changes.
Attention
We need to index the node before we can perform insertion and deletion. And it takes O(n) for indexing. So generally, insertion and deletion in linked list results in O(n). Unless the operation is performed at the head node or tail node of the linked list. It is recommended to use Array by default unless there are reasons and measurements that show using Linked List is better, refer to Are lists evil? — Bjarne Stroustrup : Standard C++ for more details.
Space Complexity
Uses more Main Memory compared to Array, because each node contains value, and also a Pointer to the next node
Single Linked List
Implement Stack and Queue (FIFO), it is O(1) to add/remove node from the tail ofLinked List, counting in the indexing time
Implement Browser History Navigation. When we at a page, knowing what is the previous page (parent node) & what is the next page(child node). This makes the browsing experience more smooth
Node Implementation
public class Node { int val; Node prev; Node next; Node() {} Node(int key, int val) this.val = val;}