## Linked Lists
* like arrays, a linked list is used to represent sequential data. it's a linear collection of data elements (nodes) whose order is not given by their physical placement in memory (as opposed to arrays where data is stored in sequential blocks of memory). instead, each element contains an address of the next element.
```python
class Node:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
```
* unlike an array, a linked list does not provide constant time access to an index (as it needs to interact through all `k` elements), however addition and removal of elements are constant time (`O(1)`).
* if you need to add or delete a node frequently, a linked list could be a good choice.
* if you need to access an element by index often, an array might be a better choice than a linked list.
* linked lists can be of the following types:
* **singled linked list**: a linked list where each node points to the next node and the last node points to `None`.
* **doubly linked list**: a linked list where each node has two pointers, `next` and `prev`. the `prev` pointer of the first node and the `next` pointer of the last node point to `None`.
* **circular linked list**: a singly linked list where the past node points back to the first node. if it's doubly, the `prev` pointer of the first node points to the last node, and the `next` pointer of the last node points to the first node.
* each node in a singly-linked list contains a value and a reference field to link to the next node. the head node (first node) usually represents the whole list.
* nodes can be added at the beginning, head needs to be updated (`current -> head` and `head = current`).
* to remove a node you set `prev.next` equal to `node.next`. if it's a double list, you also update `node.next` with `node.next.prev` to `node.prev` (and deallocate the memory).
* adding a sentinel/dummy node at the head and/or tail might help handle many edge cases where operations have to be performed at the head or the tail.
* the presence of dummy nodes ensures that operations will never be done on the head or the tail (removing the need of conditional checks to deal with `None` pointers). the only extra steps is that they need to be removed at the end of the operation.
* examples are LRU cache (where sentinel nodes are used as pseudo-head and pseudo-tail) and tree level order traversal (where sentinel nodes are used to mark level end).
* two pointers can be used to solve several problems:
* getting the kth from last node: have two pointers, where one is `k` nodes ahead of the other, when the node ahead reaches the end, the other node is `k` behind.
* detecting cycles: have two pointers, where one pointer increments twice as much as the other. if the two pointers meet, there is a cycle. if there is no cycle, the faster pointer takes `N/2` to reach the end of the list (`N` being the length).
* getting in the middle node: have two pointers, where one pointer increments twices as much as the other. when the faster node reaches the end of the list, the slower node will be at the middle.