Linked List Example Java Program

Definition

This class stores the elements in nodes that each have a pointer to the previous and next nodes in the list. The list can be traversed by following the pointers, and elements can be added or removed simply by changing the pointers around to place the node in its proper place.

Syntax

LinkedList variable_name = new LinkedList();

Linked List Example Program

import java.util.LinkedList;
public class LinkedListDemo {
    public static void main(String a[]){
        LinkedList ll = new LinkedList();
        ll.add("January");
        ll.add("March");
        ll.add("May");
        ll.add("July");
        System.out.println(ll);
        System.out.println("Size of the linked list: "+ll.size());
        System.out.println("Is LinkedList empty? "+ll.isEmpty());
        System.out.println("Does LinkedList contains 'September'? "+ll.contains("September"));
    }
}

Sample Output

Output is:
[January, March, May, July]
Size of the linked list: 4
Is LinkedList empty? false
Does LinkedList contains 'September'? false