|
| 1 | +Overview: |
| 2 | + |
| 3 | +• LinkedList in Java (java.util.LinkedList) is a doubly-linked list implementation of the List and Deque interfaces. |
| 4 | +• It stores elements as nodes where each node holds references to its prev and next nodes and the stored element. |
| 5 | +• Good for frequent insertions/removals at arbitrary positions; not as good for random access (get by index), |
| 6 | + which is O(n). |
| 7 | + |
| 8 | +Internal Structure (conceptual) |
| 9 | + |
| 10 | +Each node is roughly: |
| 11 | + |
| 12 | +class Node<E> { |
| 13 | + E item; |
| 14 | + Node<E> next; |
| 15 | + Node<E> prev; |
| 16 | +} |
| 17 | + |
| 18 | +The LinkedList maintains: |
| 19 | +• Node<E> first — reference to the head node (eldest) |
| 20 | +• Node<E> last — reference to the tail node (youngest) |
| 21 | +• int size — number of elements |
| 22 | + |
| 23 | +How basic operations work |
| 24 | +• addFirst(e) / addLast(e) — create a new node, link it into the head or tail, update first / last. O(1). |
| 25 | +• add(index, e) — find node at index (walk from head or tail depending on index), insert new node by adjusting |
| 26 | + neighbor pointers. O(n) in worst case. |
| 27 | +• removeFirst() / removeLast() — unlink head/tail node and return element. O(1). |
| 28 | +• remove(index) — find node then unlink it by adjusting neighbor pointers. O(n). |
| 29 | +• get(index) — walk from head or tail to reach index, return element. O(n). |
| 30 | +• iterator / listIterator — provide sequential access; ListIterator supports bi-directional movement. |
| 31 | + |
| 32 | +Advantages |
| 33 | +• Fast insertions and deletions when you already have node reference or at ends (O(1)). |
| 34 | +• Implements Deque so it supports stack/queue/deque operations naturally. |
| 35 | + |
| 36 | +Disadvantages |
| 37 | +• Random access (get by index) is O(n). |
| 38 | +• Extra memory per element (two pointers per node). |
| 39 | +• Poor cache locality compared to ArrayList because nodes are scattered in heap. |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +first -> [null | A | *] <-> [* | B | *] <-> [* | C | null] <- last |
| 44 | + idx0 idx1 idx2 |
| 45 | + |
| 46 | +Each node: [prev | item | next] |
| 47 | + |
| 48 | +Example code (LinkedList usage) |
| 49 | + |
| 50 | +import java.util.LinkedList; |
| 51 | +import java.util.ListIterator; |
| 52 | + |
| 53 | +public class LinkedListExample { |
| 54 | + public static void main(String[] args) { |
| 55 | + LinkedList<String> list = new LinkedList<>(); |
| 56 | + list.add("A"); // addLast |
| 57 | + list.addFirst("start"); // addFirst |
| 58 | + list.add(1, "B"); // insert at index 1 |
| 59 | + |
| 60 | + System.out.println("List: " + list); // [start, B, A] |
| 61 | + System.out.println("Removed: " + list.remove(1)); // removes "B" |
| 62 | + System.out.println("After removal: " + list); // [start, A] |
| 63 | + |
| 64 | + // iterate forward |
| 65 | + ListIterator<String> it = list.listIterator(); |
| 66 | + while (it.hasNext()) { |
| 67 | + System.out.println("Next: " + it.next()); |
| 68 | + } |
| 69 | + |
| 70 | + // iterate backward |
| 71 | + while (it.hasPrevious()) { |
| 72 | + System.out.println("Prev: " + it.previous()); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +When to use LinkedList |
| 78 | +• When your workload has many insertions/deletions at head/tail or near known nodes. |
| 79 | +• When you need a Deque implementation (queue/stack behavior) with frequent adds/removes at ends. |
| 80 | +• Avoid when you need frequent random access by index — prefer ArrayList instead. |
| 81 | + |
| 82 | +Performance summary |
| 83 | +• addFirst/addLast: O(1) |
| 84 | +• removeFirst/removeLast: O(1) |
| 85 | +• add(index)/remove(index)/get(index): O(n) |
| 86 | +• memory per element: higher (node overhead) |
0 commit comments