Codectionary / Developer documentation / Java

LinkedList

LinkedList is a doubly-linked list implementation of both the List and Deque interfaces. Unlike ArrayList, it stores elements as individual nodes linked to their neighbors, which makes inserting and removing elements at the beginning or middle much faster, at the cost of slower random access by index. Because it implements Deque, LinkedList can also be used directly as a stack or queue.

Syntax

LinkedList<Type> list = new LinkedList<>();

Examples

Basic LinkedList Usage

LinkedList supports the same core List operations as ArrayList.

import java.util.LinkedList;

LinkedList<String> tasks = new LinkedList<>();
tasks.add("Write code");
tasks.add("Test code");
tasks.add("Deploy code");

System.out.println(tasks);       // [Write code, Test code, Deploy code]
System.out.println(tasks.get(1)); // Test code

Fast Insertion at Both Ends

LinkedList excels at adding or removing from the front or back, unlike ArrayList.

import java.util.LinkedList;

LinkedList<Integer> list = new LinkedList<>();
list.add(2);
list.add(3);

list.addFirst(1);  // fast - O(1)
list.addLast(4);    // fast - O(1)

System.out.println(list);  // [1, 2, 3, 4]

list.removeFirst();
System.out.println(list);  // [2, 3, 4]

Using LinkedList as a Stack

LinkedList's Deque interface provides push/pop for stack-like (last-in-first-out) behavior.

import java.util.LinkedList;

LinkedList<String> stack = new LinkedList<>();
stack.push("first");
stack.push("second");
stack.push("third");

System.out.println(stack.pop());  // third (last one pushed, comes off first)
System.out.println(stack.pop());  // second
System.out.println(stack);         // [first]

Using LinkedList as a Queue

LinkedList's Queue methods provide first-in-first-out behavior.

import java.util.LinkedList;
import java.util.Queue;

Queue<String> queue = new LinkedList<>();
queue.offer("first");
queue.offer("second");
queue.offer("third");

System.out.println(queue.poll());  // first (first one added, comes off first)
System.out.println(queue.peek());   // second (look without removing)

Best practices

  • Choose LinkedList over ArrayList when your code frequently inserts or removes elements at the beginning or middle of the list
  • Choose ArrayList instead when you mostly need fast random access by index - LinkedList access by index is slow (O(n)) since it must walk the chain
  • Use LinkedList's Deque methods (push/pop, offer/poll) when you specifically need stack or queue behavior, rather than reimplementing that logic yourself
  • Consider ArrayDeque instead of LinkedList for pure stack/queue use cases - it is generally faster and more memory-efficient

At a glance

Purpose
General-purpose application development
File extension
.java
Runs in
Java Virtual Machine
Usually used with
JDK and Java libraries

Specifications & further reading

Related Java documentation

ArrayList
ArrayList is a resizable array implementation of the List interface, part of java.util. Unlike a plain array, an ArrayList automatically grows as elements are added, and it provides a rich set of methods for adding, removing, searching, and iterating. Since generics require object types, an ArrayList of primitives (like int) actually stores their wrapper class (Integer) via autoboxing.
HashMap
HashMap stores data as key-value pairs, offering constant-time (average case) lookup, insertion, and deletion by key, backed by a hash table. Keys must be unique - adding a value with an existing key overwrites the previous value. HashMap does not guarantee any particular ordering of its entries, unlike LinkedHashMap (which preserves insertion order) or TreeMap (which keeps keys sorted).
HashSet
HashSet is a collection that stores unique elements with no guaranteed ordering, backed by a HashMap internally. Adding a duplicate element has no effect, since HashSet automatically enforces uniqueness. It provides constant-time (average case) performance for adding, removing, and checking membership, making it ideal for deduplication and fast lookups.
TreeMap & TreeSet
TreeMap and TreeSet are sorted collections backed by a red-black tree, automatically keeping their keys (or elements) in ascending order at all times. This ordering comes at a cost - operations run in logarithmic time rather than the constant time of HashMap/HashSet - but it's invaluable when you need sorted iteration, range queries, or to quickly find the smallest or largest element.