Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

LinkedList

A doubly linked list that implements both `List` and `Deque`.

Available since: Java 1.2

Syntax

public class LinkedList<E> implements List<E>, Deque<E>

Examples

Adding to the start and to the end:

void main() {
    LinkedList<String> queue = new LinkedList<>();
    queue.add("b");
    queue.addFirst("a");
    queue.addLast("c");
    IO.println(queue);
    IO.println(queue.removeFirst() + " " + queue);
}

Output:

[a, b, c]
a [b, c]

Notes

In practice LinkedList is rarely the right choice. Index access get(i) walks the chain of nodes and is slow, and every element takes more memory. For a list ArrayList is usually better; for a queue or a stack use ArrayDeque.

See also

  • ArrayList — A resizable list backed by an array — the most common `List` implementation.
  • ArrayDeque — A fast array-based double-ended queue — a replacement for `Stack` and `LinkedList`.
  • Deque — A double-ended queue: elements can be added and taken at both ends.
  • List.getFirst() — Returns the first or the last element of an ordered collection.

In the tutorial

  • Lists: List and ArrayList — Lists in Java: ArrayList, adding, reading and removing elements, unmodifiable List.of lists and lists of numbers.

Thanks for reading!

Donate to the site — we work hard for you.