Linked list

A  is a linear collection of elements that may spread across disjoint positions in the memory. The access to and traversal on elements on the linked lists are given by adjacency pointers.

Singly linked list


A singly linked list is the basic form of linked lists. Each element has a pointer to the next element. The last element's pointer to next element is a null pointer. A singly linked list may only be traversed from its head, going through each node one-by-one.

Doubly linked list


In a doubly linked list, each element has a pointer to its previous and next element. The first element's prev pointer and the last element's next pointer point to null. A doubly linked list can be traversed from either end.

Cyclic linked list


In a cyclic linked list, or a circularly linked list, one of its nodes points to the head or a middle node, thus forming a cycle. Hence, a cyclic linked list does not have an actual tail.

Multi linked list
In a multi linked list, each node has two or more link fields, each connecting to the same set of data records in different orders.

Doubly linked lists are an example of multiply linked lists. However, because of the nature that each two adjacent nodes have links to each other in opposite directions, they are handled as a separate case.


 * External references

Key points in coding

 * 1) When dereferencing a node pointer / reference, make sure to check that it is not null.
 * 2) Never lose access to the head. This is essential when the head will change.
 * 3) Make sure to disconnect appropriate nodes when needed, even when it does not affect correctness. (✔ good practice)

Sample problems

 * Reverse a linked list
 * Reverse a linked list II
 * Swap nodes in pairs
 * Middle of the linked list
 * Reorder list
 * Merge two sorted linked lists
 * Merge sort a linked list
 * Partition list
 * Linked list cycle
 * Linked list cycle II

Programming language APIs

 * Java
 * Java 7:
 * C++
 * Doubly linked list:
 * Singly linked list:  (since C++11)