Java LinkedList Class Tutorial

The LinkedList class in Java is part of the Java Collections Framework and provides a doubly-linked list implementation. This guide covers various methods provided by the LinkedList class, including their usage and examples.

Table of Contents

  1. Introduction
  2. Basic Methods
    • add(E e)
    • add(int index, E element)
    • addAll(Collection<? extends E> c)
    • addAll(int index, Collection<? extends E> c)
    • addFirst(E e)
    • addLast(E e)
  3. Access Methods
    • get(int index)
    • getFirst()
    • getLast()
  4. Search Methods
    • contains(Object o)
    • indexOf(Object o)
    • lastIndexOf(Object o)
  5. Update Methods
    • set(int index, E element)
  6. Remove Methods
    • remove()
    • remove(int index)
    • remove(Object o)
    • removeFirst()
    • removeLast()
    • removeFirstOccurrence(Object o)
    • removeLastOccurrence(Object o)
  7. Conversion Methods
    • toArray()
    • toArray(T[] a)
  8. Other Methods
    • size()
    • clear()
    • clone()
    • descendingIterator()
    • element()
    • listIterator()
    • offer(E e)
    • offerFirst(E e)
    • offerLast(E e)
    • peek()
    • peekFirst()
    • peekLast()
    • poll()
    • pollFirst()
    • pollLast()
    • pop()
    • push(E e)
  9. Conclusion

1. Introduction

The Java LinkedList class is part of the Collection Framework and is implemented as a doubly linked list. Each element in the list is connected to both the previous and the next elements. This structure allows efficient insertion and deletion operations, especially in the middle or at the beginning of the list.

LinkedList implements both the List and Deque interfaces, making it versatile. It can function as a regular list, a queue in a first-in first-out manner, or a stack in a last-in first-out manner. This provides multiple ways to manage and access data.

One of the key characteristics of LinkedList is that it maintains the order of elements. The order in which elements are added is preserved throughout the list. This remains true even after elements are removed or new elements are inserted.

While LinkedList excels at insertion and deletion, accessing elements by index is slower compared to ArrayList. The list must be traversed from the start to reach a particular element. This makes it less efficient for frequent random access.

LinkedList can store duplicate elements and null values. It does not have a fixed size, which means it can grow or shrink as elements are added or removed. This offers flexibility in how data is managed within the list.

2. Basic Methods

add(E e) Method

Adds the specified element to the end of the list.

Example

import java.util.LinkedList;

public class LinkedListAddExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");
        System.out.println("LinkedList: " + list);
    }
}

Output:

LinkedList: [Apple, Banana, Orange]

add(int index, E element) Method

Inserts the specified element at the specified position in the list.

Example

import java.util.LinkedList;

public class LinkedListAddAtIndexExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Orange");
        list.add(1, "Banana");
        System.out.println("LinkedList: " + list);
    }
}

Output:

LinkedList: [Apple, Banana, Orange]

addAll(Collection<? extends E> c) Method

Adds all the elements in the specified collection to the end of the list.

Example

import java.util.LinkedList;
import java.util.ArrayList;

public class LinkedListAddAllExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");

        ArrayList<String> moreFruits = new ArrayList<>();
        moreFruits.add("Orange");
        moreFruits.add("Grapes");

        list.addAll(moreFruits);
        System.out.println("LinkedList: " + list);
    }
}

Output:

LinkedList: [Apple, Banana, Orange, Grapes]

addAll(int index, Collection<? extends E> c) Method

Adds all the elements in the specified collection starting at the specified position in the list.

Example

import java.util.LinkedList;
import java.util.ArrayList;

public class LinkedListAddAllAtIndexExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");

        ArrayList<String> moreFruits = new ArrayList<>();
        moreFruits.add("Orange");
        moreFruits.add("Grapes");

        list.addAll(1, moreFruits);
        System.out.println("LinkedList: " + list);
    }
}

Output:

LinkedList: [Apple, Orange, Grapes, Banana]

addFirst(E e) Method

Inserts the specified element at the beginning of the list.

Example

import java.util.LinkedList;

public class LinkedListAddFirstExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.addFirst("Orange");
        System.out.println("LinkedList: " + list);
    }
}

Output:

LinkedList: [Orange, Apple, Banana]

addLast(E e) Method

Appends the specified element to the end of the list.

Example

import java.util.LinkedList;

public class LinkedListAddLastExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.addLast("Orange");
        System.out.println("LinkedList: " + list);
    }
}

Output:

LinkedList: [Apple, Banana, Orange]

3. Access Methods

get(int index) Method

Returns the element at the specified position in the list.

Example

import java.util.LinkedList;

public class LinkedListGetExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        String element = list.get(1);
        System.out.println("Element at index 1: " + element);
    }
}

Output:

Element at index 1: Banana

getFirst() Method

Returns the first element in the list.

Example

import java.util.LinkedList;

public class LinkedListGetFirstExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        String firstElement = list.getFirst();
        System.out.println("First element: " + firstElement);
    }
}

Output:

First element: Apple

getLast() Method

Returns the last element in the list.

Example

import java.util.LinkedList;

public class LinkedListGetLastExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        String lastElement = list.getLast();
        System.out.println("Last element: " + lastElement);
    }
}

Output:

Last element: Orange

4. Search Methods

contains(Object o) Method

Returns true if the list contains the specified element.

Example

import java.util.LinkedList;

public class LinkedListContainsExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        boolean contains = list.contains("Banana");
        System.out.println("Contains 'Banana': " + contains);
    }
}

Output:

Contains 'Banana': true

indexOf(Object o) Method

Returns the index of the first occurrence of the specified element in the list, or -1 if the list does not contain the element.

Example

import java.util.LinkedList;

public class LinkedListIndexOfExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        int index = list.indexOf("Banana");
        System.out.println("Index of 'Banana': " + index);
    }
}

Output:

Index of 'Banana': 1

lastIndexOf(Object o) Method

Returns the index of the last occurrence of the specified element in the list, or -1 if the list does not contain the element.

Example

import java.util.LinkedList;

public class LinkedListLastIndexOfExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");
        list.add("Banana");

        int lastIndex = list.lastIndexOf("Banana");
        System.out.println("Last index of 'Banana': " + lastIndex);
    }
}

Output:

Last index of ‘Banana’: 3

5. Update Methods

set(int index, E element)

Replaces the element at the specified position in the list with the specified element.

Example


import java.util.LinkedList;

public class LinkedListSetExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        String replacedElement = list.set(1, "Mango");
        System.out.println("Replaced element: " + replacedElement);
        System.out.println("Updated LinkedList: " + list);
    }
}

Output:

Replaced element: Banana
Updated LinkedList: [Apple, Mango, Orange]

6. Remove Methods

remove() Method

Retrieves and removes the head (first element) of this list.

Example

import java.util.LinkedList;

public class LinkedListRemoveExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        String removedElement = list.remove();
        System.out.println("Removed element: " + removedElement);
        System.out.println("Updated LinkedList: " + list);
    }
}

Output:

Removed element: Apple
Updated LinkedList: [Banana, Orange]

remove(int index) Method

Removes the element at the specified position in this list.

Example

import java.util.LinkedList;

public class LinkedListRemoveAtIndexExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        String removedElement = list.remove(1);
        System.out.println("Removed element: " + removedElement);
        System.out.println("Updated LinkedList: " + list);
    }
}

Output:

Removed element: Banana
Updated LinkedList: [Apple, Orange]

remove(Object o) Method

Removes the first occurrence of the specified element from this list, if it is present.

Example

import java.util.LinkedList;

public class LinkedListRemoveObjectExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        boolean isRemoved = list.remove("Banana");
        System.out.println("Was 'Banana' removed? " + isRemoved);
        System.out.println("Updated LinkedList: " + list);
    }
}

Output:

Was 'Banana' removed? true
Updated LinkedList: [Apple, Orange]

removeFirst() Method

Retrieves and removes the first element of this list.

Example

import java.util.LinkedList;

public class LinkedListRemoveFirstExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        String removedElement = list.removeFirst();
        System.out.println("Removed first element: " + removedElement);
        System.out.println("Updated LinkedList: " + list);
    }
}

Output:

Removed first element: Apple
Updated LinkedList: [Banana, Orange]

removeLast() Method

Retrieves and removes the last element of this list.

Example

import java.util.LinkedList;

public class LinkedListRemoveLastExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        String removedElement = list.removeLast();
        System.out.println("Removed last element: " + removedElement);
        System.out.println("Updated LinkedList: " + list);
    }
}

Output:

Removed last element: Orange
Updated LinkedList: [Apple, Banana]

removeFirstOccurrence(Object o) Method

Removes the first occurrence of the specified element from this list, if it is present.

Example

import java.util.LinkedList;

public class LinkedListRemoveFirstOccurrenceExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");
        list.add("Banana");

        boolean isRemoved = list.removeFirstOccurrence("Banana");
        System.out.println("Was 'Banana' removed? " + isRemoved);
        System.out.println("Updated LinkedList: " + list);
    }
}

Output:

Was 'Banana' removed? true
Updated LinkedList: [Apple, Orange, Banana]

removeLastOccurrence(Object o) Method

Removes the last occurrence of the specified element from this list, if it is present.

Example

import java.util.LinkedList;

public class LinkedListRemoveLastOccurrenceExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");
        list.add("Banana");

        boolean isRemoved = list.removeLastOccurrence("Banana");
        System.out.println("Was 'Banana' removed? " + isRemoved);
        System.out.println("Updated LinkedList: " + list);
    }
}

Output:

Was 'Banana' removed? true
Updated LinkedList: [Apple, Banana, Orange]

7. Conversion Methods

toArray() Method

Returns an array containing all of the elements in this list in proper sequence (from first to last element).

Example

import java.util.LinkedList;

public class LinkedListToArrayExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        Object[] array = list.toArray();
        System.out.println("Array elements:");
        for (Object element : array) {
            System.out.println(element);
        }
    }
}

Output:

Array elements:
Apple
Banana
Orange

toArray(T[] a) Method

Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.

Example

import java.util.LinkedList;

public class LinkedListToTypedArrayExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        String[] array = list.toArray(new String[0]);
        System.out.println("Array elements:");
        for (String element : array) {
            System.out.println(element);
        }
    }
}

Output:

Array elements:
Apple
Banana
Orange

8. Other Methods

size() Method

Returns the number of elements in this list.

Example

import java.util.LinkedList;

public class LinkedListSizeExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        int size = list.size();
        System.out.println("Size of LinkedList: " + size);
    }
}

Output:

Size of LinkedList: 3

clear() Method

Removes all of the elements from this list.

Example

import java.util.LinkedList;

public class LinkedListClearExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        list.clear();
        System.out.println("LinkedList after clear: " + list);
    }
}

Output:

LinkedList after clear: []

clone() Method

Returns a shallow copy of this LinkedList.

Example

import java.util.LinkedList;

public class LinkedListCloneExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        LinkedList<String> clonedList = (LinkedList<String>) list.clone();
        System.out.println("Cloned LinkedList: " + clonedList);
    }
}

Output:

Cloned LinkedList: [Apple, Banana, Orange]

descendingIterator() Method

Returns an iterator over the elements in this deque in reverse sequential order.

Example

import java.util.LinkedList;
import java.util.Iterator;

public class LinkedListDescendingIteratorExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        Iterator<String> iterator = list.descendingIterator();
        System.out.println("LinkedList in reverse order:");
        while (iterator.hasNext())

 {
            System.out.println(iterator.next());
        }
    }
}

Output:

LinkedList in reverse order:
Orange
Banana
Apple

element() Method

Retrieves, but does not remove, the head (first element) of this list.

Example

import java.util.LinkedList;

public class LinkedListElementExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        String element = list.element();
        System.out.println("Head element: " + element);
    }
}

Output:

Head element: Apple

listIterator() Method

Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.

Example

import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListListIteratorExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        ListIterator<String> iterator = list.listIterator(1);
        System.out.println("LinkedList elements from index 1:");
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

Output:

LinkedList elements from index 1:
Banana
Orange

offer(E e) Method

Inserts the specified element at the end of this list.

Example

import java.util.LinkedList;

public class LinkedListOfferExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");

        boolean isAdded = list.offer("Orange");
        System.out.println("Was 'Orange' added? " + isAdded);
        System.out.println("LinkedList: " + list);
    }
}

Output:

Was 'Orange' added? true
LinkedList: [Apple, Banana, Orange]

offerFirst(E e) Method

Inserts the specified element at the front of this list.

Example

import java.util.LinkedList;

public class LinkedListOfferFirstExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");

        boolean isAdded = list.offerFirst("Orange");
        System.out.println("Was 'Orange' added to the front? " + isAdded);
        System.out.println("LinkedList: " + list);
    }
}

Output:

Was 'Orange' added to the front? true
LinkedList: [Orange, Apple, Banana]

offerLast(E e) Method

Inserts the specified element at the end of this list.

Example

import java.util.LinkedList;

public class LinkedListOfferLastExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");

        boolean isAdded = list.offerLast("Orange");
        System.out.println("Was 'Orange' added to the end? " + isAdded);
        System.out.println("LinkedList: " + list);
    }
}

Output:

Was 'Orange' added to the end? true
LinkedList: [Apple, Banana, Orange]

peek() Method

Retrieves, but does not remove, the head (first element) of this list, or returns null if this list is empty.

Example

import java.util.LinkedList;

public class LinkedListPeekExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        String element = list.peek();
        System.out.println("Head element: " + element);
    }
}

Output:

Head element: Apple

peekFirst() Method

Retrieves, but does not remove, the first element of this list, or returns null if this list is empty.

Example

import java.util.LinkedList;

public class LinkedListPeekFirstExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        String element = list.peekFirst();
        System.out.println("First element: " + element);
    }
}

Output:

First element: Apple

peekLast() Method

Retrieves, but does not remove, the last element of this list, or returns null if this list is empty.

Example

import java.util.LinkedList;

public class LinkedListPeekLastExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        String element = list.peekLast();
        System.out.println("Last element: " + element);
    }
}

Output:

Last element: Orange

poll() Method

Retrieves and removes the head (first element) of this list, or returns null if this list is empty.

Example

import java.util.LinkedList;

public class LinkedListPollExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        String element = list.poll();
        System.out.println("Removed head element: " + element);
        System.out.println("Updated LinkedList: " + list);
    }
}

Output:

Removed head element: Apple
Updated LinkedList: [Banana, Orange]

pollFirst() Method

Retrieves and removes the first element of this list, or returns null if this list is empty.

Example

import java.util.LinkedList;

public class LinkedListPollFirstExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        String element = list.pollFirst();
        System.out.println("Removed first element: " + element);
        System.out.println("Updated LinkedList: " + list);
    }
}

Output:

Removed first element: Apple
Updated LinkedList: [Banana, Orange]

pollLast() Method

Retrieves and removes the last element of this list, or returns null if this list is empty.

Example

import java.util.LinkedList;

public class LinkedListPollLastExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        String element = list.pollLast();
        System.out.println("Removed last element: " + element);
        System.out.println("Updated LinkedList: " + list);
    }
}

Output:

Removed last element: Orange
Updated LinkedList: [Apple, Banana]

pop() Method

Pops an element from the stack represented by this list. In other words, removes and returns the first element of this list.

Example

import java.util.LinkedList;

public class LinkedListPopExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        String element = list.pop();
        System.out.println("Popped element: " + element);
        System.out.println("Updated LinkedList: " + list);
    }
}

Output:

Popped element: Apple
Updated LinkedList: [Banana, Orange]

push(E e) Method

Pushes an element onto the stack represented by this list. In other words, inserts the element at the front of this list.

Example

import java.util.LinkedList;

public class LinkedListPushExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");

        list.push("Orange");
        System.out.println("Updated LinkedList: " + list);
    }
}

Output:

Updated LinkedList: [Orange, Apple, Banana]

9. Conclusion

The LinkedList class in Java provides a comprehensive set of methods for managing elements in a linked list. By understanding and utilizing these methods, you can efficiently perform various operations on linked lists in your Java applications. This guide has covered the most commonly used methods with examples to help you get started.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top