Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Java LinkedList programs and expected output

Last updated: 29 Sept 20264 min read
tutorial
By AITrove Editorial

A complete LinkedList program makes mutation order visible through its printed results.

Endpoint queue

Java
import java.util.LinkedList;

public class TaskQueueDemo {
    public static void main(String[] args) {
        LinkedList<String> pendingTasks = new LinkedList<>();
        pendingTasks.addLast("Validate invoice");
        pendingTasks.addLast("Send receipt");
        pendingTasks.addFirst("Check account");
        System.out.println(pendingTasks);
        System.out.println(pendingTasks.pollFirst());
        System.out.println(pendingTasks.peekFirst());
        System.out.println(pendingTasks.size());
    }
}
Output
[Check account, Validate invoice, Send receipt]
Check account
Validate invoice
2

Insertion after a matching stage

Java
import java.util.Arrays;
import java.util.LinkedList;
import java.util.ListIterator;

public class ListEditDemo {
    public static void main(String[] args) {
        LinkedList<String> stages = new LinkedList<>(
            Arrays.asList("Received", "Packed", "Dispatched"));
        ListIterator<String> cursor = stages.listIterator();
        while (cursor.hasNext()) {
            if ("Packed".equals(cursor.next())) {
                cursor.add("Quality checked");
                break;
            }
        }
        System.out.println(stages);
    }
}
Output
[Received, Packed, Quality checked, Dispatched]

The queue uses endpoints. The stage editor locates a matching value before it inserts. Both return an ordered sequence, but their costs differ because the second program searches.

Connected lessons

Continue with Java LinkedList: operations, internals and failure cases, Java LinkedList quiz.

Common Mistakes

Compile each public class in a file of the same name. These examples are single-threaded. Do not mistake printed output for a persistence or delivery guarantee.

Read the operation order as a state trace

Write the list after each mutation before checking the final print. addFirst and addLast place values at different ends, and pollFirst removes the value that currently occupies the head. Duplicate descriptions remain separate elements because this example has no identity or deduplication rule.

The ListIterator editor first searches for a matching stage. Once positioned, add inserts relative to that cursor. Locating the stage can visit the whole list, so the program’s total cost includes search even though changing the nearby links requires constant work.

Compare a missing stage and an empty list with the printed success case. Neither should create a quality-check stage at a guessed position unless that fallback is part of the contract. A null stage also needs a stated input policy; invoking a method on a null member will not be repaired by using a linked representation.

java
linkedlist-examples
Storage details