Java LinkedList Declaration: Syntax and Common Pitfalls
Learn how to declare a LinkedList in Java correctly: syntax, type parameters, initialization options, and common pitfalls to avoid.

Declaring a LinkedList in Java requires choosing an element type, using the correct constructor, and deciding whether to expose the List interface or the concrete class. The examples below cover the syntax and common mistakes.
The Basic Declaration Syntax
In Java, a LinkedList is declared with a type parameter that specifies the element type. The simplest declaration uses the diamond operator to avoid repeating the type on the right side:
LinkedList<String> names = new LinkedList<>();This creates an empty LinkedList that holds String objects. The left side of the assignment names the variable and its type; the right side invokes the no-argument constructor. The diamond operator (<>) tells the compiler to infer the type argument from the left side, so you do not have to write new LinkedList<String>().
You can also declare the variable without initializing it immediately:
LinkedList<Integer> numbers;
numbers = new LinkedList<>();This is useful when the list is assigned later, for example inside a conditional branch. However, the variable must be definitely assigned before use, or the compiler will reject the code.
Choosing the Right Type Parameter
The type parameter defines what objects the list can hold. Using a concrete type such as String or Integer gives you compile-time type safety. If you try to add an incompatible object, the code will not compile.
LinkedList<String> words = new LinkedList<>();
words.add("hello");
// words.add(42); // compile errorOmitting the type parameter, known as a raw type, is legal but discouraged:
LinkedList raw = new LinkedList();A raw LinkedList can hold any Object, but every retrieval returns an Object reference, forcing casts and bypassing the compiler's type checks. Modern code should always specify a type argument or use the diamond operator.
You can also declare a LinkedList of a custom class, an interface, or even another generic type:
LinkedList<Map<String, Integer>> configs = new LinkedList<>();This works because the type parameter can be any reference type, including parameterized types.
Declaring with Initial Elements
The no-argument constructor creates an empty list. If you know the initial elements, you can pass a collection to the constructor:
List<String> initial = Arrays.asList("a", "b", "c");
LinkedList<String> letters = new LinkedList<>(initial);The constructor copies the elements from the provided collection into the new LinkedList. This is useful when you need to convert an existing ArrayList or any Collection into a linked list.
Java 9 introduced List.of, which creates an immutable list. You can use it similarly:
LinkedList<String> fixed = new LinkedList<>(List.of("x", "y"));The LinkedList itself remains mutable; only the source collection is immutable.
There is no constructor that accepts an initial capacity, unlike ArrayList. This is because a linked list does not allocate a contiguous backing array; each element is a separate node. Attempting to pre-size a LinkedList would not improve performance.
Declaring as List or LinkedList
A common decision is whether to declare the variable as the concrete LinkedList type or as the List interface. The interface type is often preferred because it allows you to swap the implementation later without changing client code:
List<String> words = new LinkedList<>();Here the variable's type is List, and the actual object is a LinkedList. This works because LinkedList implements List. If you later decide an ArrayList is more appropriate, you can change only the right side of the assignment.
Declaring as LinkedList gives you access to methods that are not part of the List interface, such as addFirst, addLast, removeFirst, and removeLast. If your code relies on those methods, you must use the concrete type:
LinkedList<String> deque = new LinkedList<>();
deque.addFirst("first");If you declare the variable as List, those methods are not visible, even though the underlying object supports them. Choose the type based on whether you need the linked-list-specific operations.
Common Declaration Mistakes
One frequent mistake is forgetting the diamond operator and writing new LinkedList() on the right side while the left side uses a generic type. This produces an unchecked warning and can lead to ClassCastException at runtime if the code later casts elements to an unintended type.
Another mistake is a missing or incorrect import. The standard LinkedList lives in java.util, so ensure your import statement reads:
import java.util.LinkedList;If the wrong package is imported, the compiler will not find the class you expect, so verify the package name.
A more subtle issue is declaring a LinkedList of a primitive type, such as int. Java generics do not support primitives, so you must use the wrapper class Integer. Autoboxing handles conversion in most cases, but the type parameter must be a reference type.
Performance and Memory Considerations
The declaration syntax has no direct performance cost, but the choice of LinkedList over other List implementations affects runtime behavior. A LinkedList stores each element in a node that also holds references to the previous and next nodes. This adds memory overhead compared to an ArrayList, which stores elements in a contiguous array.
Insertions and removals at the beginning and end of a LinkedList are O(1). Middle operations through the List interface are O(n) because the list must be traversed to the target position. If you are already positioned with a ListIterator, its add and remove methods are O(1). Indexed access is O(n) because it requires traversal from one end of the list. In contrast, ArrayList provides O(1) indexed access, but inserting or removing elements often requires shifting elements and can be O(n).
If your code frequently adds elements to the front of the list, a LinkedList is often a better choice than an ArrayList. If you primarily access elements by index, an ArrayList is usually more efficient. The declaration does not change these characteristics; it only determines which implementation you are using.
When to Use a LinkedList
Use a LinkedList when you need a doubly-linked list that supports efficient insertion and removal at both ends, or when you need queue or deque behavior. The LinkedList class implements both List and Deque, so it can serve as a double-ended queue.
If your algorithm frequently adds and removes elements from the middle, a LinkedList is efficient only when you are iterating with a ListIterator and not using index-based operations. If you need random access by index, prefer an ArrayList.
For most general-purpose list use cases, ArrayList is the default recommendation because it has better cache locality and lower memory overhead. Reserve LinkedList for scenarios where its specific operations, such as addFirst and removeLast, are central to the design.
Try matching browser tools
All toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesJava Final Keyword: Usage and Behavior
How Java's final keyword works for variables, methods, and classes, with practical examples and decision guidance for using it.
Java Final Variable: Syntax, Behavior, and Use Cases
Understand what a final variable in Java means, how initialization works, and when to use it for constants, immutability, and clearer code.
Java Duration vs Period: When to Use Each
java duration vs period: Compare Java Duration and Period to handle time-based and date-based calculations correctly. Learn syntax, conversion, and pitfalls.
Instance vs Static Variables in Java: Key Differences
Understand the difference between instance and static variables in Java, including memory, lifecycle, and when to use each.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.