Java Programming Handbook

    Java LinkedList

    The LinkedList class in Java is part of the Java Collections Framework and implements both the List and Deque interfaces. It provides a doubly-linked list data structure.

    1. What is a LinkedList?#

    • LinkedList is a doubly-linked list.
    • It allows elements to be added or removed from both ends.
    • It can store duplicate and null elements.
    List<String> list = new LinkedList<>();

    2. Internal Working#

    Doubly LinkedList
    • Each node contains a data element, a reference to the previous node, and a reference to the next node.
    • Enables constant-time insertions and removals using iterators.

    3. Constructors#

    LinkedList() // Default constructor LinkedList(Collection<? extends E>) // Constructs a list containing elements of the given collection

    4. Commonly Used Methods with Examples#

    1. add(E e) – Adds element at the end#

    LinkedList<String> list = new LinkedList<>(); list.add("Apple"); System.out.println(list);

    Output:

    [Apple]

    2. add(int index, E element) – Inserts at a specific index#

    list.add(0, "Banana"); System.out.println(list);

    Output:

    [Banana, Apple]

    3. addFirst(E e) – Adds to the beginning#

    list.addFirst("Mango"); System.out.println(list);

    Output:

    [Mango, Banana, Apple]

    4. addLast(E e) – Adds to the end#

    list.addLast("Grapes"); System.out.println(list);

    Output:

    [Mango, Banana, Apple, Grapes]

    5. get(int index) – Retrieves element at a given index#

    String item = list.get(2); System.out.println(item);

    Output:

    Apple

    6. getFirst() – Gets the first element#

    System.out.println(list.getFirst());

    Output:

    Mango

    7. getLast() – Gets the last element#

    System.out.println(list.getLast());

    Output:

    Grapes

    8. remove() – Removes and returns the first element#

    list.remove(); System.out.println(list);

    Output:

    [Banana, Apple, Grapes]

    9. remove(int index) – Removes element at a given index#

    list.remove(1); System.out.println(list);

    Output:

    [Banana, Grapes]

    10. removeFirst() – Removes the first element#

    list.removeFirst(); System.out.println(list);

    Output:

    [Grapes]

    11. removeLast() – Removes the last element#

    list.removeLast(); System.out.println(list);

    Output:

    []

    12. contains(Object o) – Checks if an element exists#

    Output:

    true

    13. size() – Returns the number of elements in the list#

    System.out.println(list.size());

    Output:

    1

    14. isEmpty() – Checks if the list is empty#

    System.out.println(list.isEmpty());

    Output:

    false

    15. clear() – Removes all elements#

    list.clear(); System.out.println(list);

    Output:

    []

    16. offer(E e) – Adds element at the end (queue style)#

    list.offer("Kiwi"); System.out.println(list);

    Output:

    [Kiwi]

    17. peek() – Retrieves the head element without removing it#

    System.out.println(list.peek());

    Output:

    Kiwi

    18. poll() – Retrieves and removes the head element#

    System.out.println(list.poll()); System.out.println(list);

    Output:

    Kiwi []

    19. descendingIterator() – Iterates in reverse order#

    list.add("One"); list.add("Two"); list.add("Three"); Iterator<String> desc = list.descendingIterator(); while (desc.hasNext()) { System.out.print(desc.next() + " "); }

    Output:

    Three Two One

    5. Performance#

    OperationTime Complexity
    Access by indexO(n)
    Add/remove at endsO(1)
    Add/remove in middleO(n)

    6. Thread Safety#

    • LinkedList is not synchronized.
    • For a thread-safe version:
    List<String> syncList = Collections.synchronizedList(new LinkedList<>());

    7. When to Use LinkedList?#

    • When frequent insertion or removal is required.
    • When random access is not a priority.
    • For implementing queues, stacks, and deques.

    8. Best Practices#

    • Prefer using the List interface:
    List<String> names = new LinkedList<>();
    • Avoid frequent index-based access when performance is critical.

    Conclusion#

    The LinkedList is a versatile structure for dynamic data handling, especially where frequent modifications are needed. It shines in scenarios requiring sequential access and frequent insertions or deletions.

    Last updated on Apr 09, 2025