← Back to Core Java Course | Chapter 11: Collections | Lesson 5 of 17

Java LinkedList

A LinkedList is a chain of items where each one points to the next, like a treasure hunt with clues. It is quick to add or remove items in the middle.
Syntax
java
LinkedList<Type> list = new LinkedList<>();
list.add(item);
list.addFirst(item);
list.removeLast();

Introduction to LinkedList

LinkedList implements List using a doubly-linked chain of nodes rather than a contiguous array, so each element only needs to know its neighbors, not a fixed memory block.

Example: Introduction to LinkedList

java
import java.util.LinkedList;
public class Main {
	public static void main(String[] args) {
		LinkedList<String> list = new LinkedList<>(); // chain of nodes
		list.add("a");
		list.add("b");
		System.out.println(list);
	}
}

LinkedList as a Queue / Deque

Because it also implements Deque, LinkedList can be used directly as a queue (FIFO, via offer()/poll()) or a stack (LIFO, via push()/pop()) without needing a separate data structure.

Example: LinkedList as a Queue / Deque

java
import java.util.LinkedList;
public class Main {
	public static void main(String[] args) {
		LinkedList<Integer> queue = new LinkedList<>();
		queue.offer(1); // FIFO
		queue.offer(2);
		System.out.println(queue.poll());
		LinkedList<Integer> stack = new LinkedList<>();
		stack.push(1); // LIFO
		stack.push(2);
		System.out.println(stack.pop());
	}
}

Inserting and Removing Nodes

Inserting or removing a node is fast (O(1)) once you already have a reference to that position, since it's just a matter of relinking neighboring pointers — no shifting of other elements required.

Example: Inserting and Removing Nodes

java
import java.util.LinkedList;
public class Main {
	public static void main(String[] args) {
		LinkedList<String> list = new LinkedList<>();
		list.add("a");
		list.add("c");
		list.add(1, "b"); // fast insert once at that position
		System.out.println(list);
	}
}

Iterating over LinkedList

Iterating a LinkedList works the same way syntactically as an ArrayList (for-each or Iterator), but random access by index is slow (O(n)) since the list has to walk node-by-node from one end to reach a given position.

Example: Iterating over LinkedList

java
import java.util.LinkedList;
public class Main {
	public static void main(String[] args) {
		LinkedList<String> list = new LinkedList<>();
		list.add("a");
		list.add("b");
		for (String item : list) { // same syntax as ArrayList
			System.out.println(item);
		}
	}
}

ArrayList vs LinkedList

Choose ArrayList when you mostly read by index; choose LinkedList when you mostly insert/remove at the ends or in the middle via an iterator — picking the wrong one for your access pattern can hurt performance significantly.

Example: ArrayList vs LinkedList

java
import java.util.ArrayList;
import java.util.LinkedList;
public class Main {
	public static void main(String[] args) {
		ArrayList<Integer> arrayList = new ArrayList<>(); // best for index reads
		LinkedList<Integer> linkedList = new LinkedList<>(); // best for insert/remove at ends
		arrayList.add(1);
		linkedList.addFirst(1);
		System.out.println(arrayList.get(0));
		System.out.println(linkedList.getFirst());
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using get(index) in a loop on a LinkedList, which is slow (O(n)) each time.
  2. Using LinkedList for random access, when ArrayList is better.
  3. Calling removeFirst() on an empty list, which throws NoSuchElementException.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.