LinkedList Example Java

LinkedList Example Java is commonly used DS in every programming language. Explained all operations of adding elements, retrieve elements etc.

In this program, the general methods of linked list are illustrated like poll(), peek(), offer(), size(), set(), addFirst(), addLast(), removeFirst(), removeLast(), remove(), getFirst(), getLast(), get() and isEmpty().

LinkedList Example Java
import java.util.*;
public class LinkedListGeneral
  public static void main(String args[])
    LinkedList myList = new LinkedList();
    System.out.println("isEmpty() before adding elements: " + myList.isEmpty());

    myList.add(1, 'B');
    myList.set(3, false);
    System.out.println("No. of elements: " + myList.size());
    System.out.println("isEmpty() after adding elements: " + myList.isEmpty());

    System.out.println("\ngetFirst(): " + myList.getFirst());
    System.out.println("getLast(): " + myList.getLast());
    System.out.println("get(3): " + myList.get(3));

    System.out.println("\npeek(): " + myList.peek());
    System.out.println("poll(): " + myList.poll());
    System.out.println("poll(): " + myList.poll());

    System.out.println("\nElements before removal: " + myList);
    System.out.println("remove(4): " + myList.remove(4));
    System.out.println("removeFirst(): " + myList.removeFirst());
    System.out.println("removeLast(): " + myList.removeLast());
    System.out.println("Elements after removal: " + myList);

Output screen on LinkedList Example Java

LinkedList myList = new LinkedList();
System.out.println(“isEmpty() before adding elements: ” + myList.isEmpty());

A non-generic LinkedList object myList is created and checked for its emptiness. The isEmpty() method, inherited from Collection interface, checks for the existence of elements. If elements does not exist, the method returns true, else false. The above statement returns true as elements are not added yet to linked list.


Two methods for addition of elements exist – offer() method inherited from Queue interface and add() method inherited from Collection interface.

myList.set(3, false);

addFirst() method adds the element at the beginning of the list elements and addLast() adds at the last. The set() method, inherited from List interface, replaces the element. In the above code, the existing 3rd element is replaced with false. That is, the original 3rd element is lost. addFirst() and addLast() are defined in LinkedLIst itself.

System.out.println(“No. of elements: ” + myList.size());

The size() method returns the number of elements present in the linked list.

System.out.println(“\ngetFirst(): ” + myList.getFirst());
System.out.println(“getLast(): ” + myList.getLast());
System.out.println(“get(3): ” + myList.get(3));

getFirst() and getLast() methods defined in LinkedList class return the first and last elements and get() method, inherited from List interface, returns the element at the index number passed as parameter. Remember, the index numbers start from 0.

System.out.println(“\npeek(): ” + myList.peek());
System.out.println(“poll(): ” + myList.poll());

The peek() and poll() methods, inherited from Queue interface, return the head element but with a small difference. The peek() method returns the element but not deletes it. Calling the peek() method multiple times returns the same element. The poll() method returns and also deletes the element in the list. Calling the poll() method number of times, returns different elements (as elements are deleted). Observe, the screenshot when poll() is called two times, it printed two different values.

The removeFirst() and removeLast() methods deletes the head and tail elements. The remove() method deletes the element whose index is passed as parameter.

Note: This program gives a compilation warning as the linked list is not designed for generics. The other programs on LinkedList do not as they are designed for generics.

2 thoughts on “LinkedList Example Java”

Leave a Comment

Your email address will not be published.