LinkedList in Java

Linked List are linear data structures where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The elements are linked using pointers and addresses. Each element is known as a node. Due to the dynamicity and ease of insertions and deletions, they are preferred over the arrays. It also has few disadvantages like the nodes cannot be accessed directly instead we need to start from the head and follow through the link to reach to a node we wish to access.
To store the elements in a linked list we use a doubly linked list which provides a linear data structure and also used to inherit an abstract class and implement list and deque interfaces.

In Java, LinkedList class implements the list interface. The LinkedList class also consists of various constructors and methods like other java collections.

Constructors for Java LinkedList:

  1. LinkedList(): Used to create an empty linked list.
  2. LinkedList(Collection C): Used to create a ordered list which contains all the elements of a specified collection, as returned by the collection’s iterator.


// Java code for Linked List implementation
  
import java.util.*;
  
public class Test
{
    public static void main(String args[])
    {
        // Creating object of class linked list
        LinkedList<String> object = new LinkedList<String>();
  
        // Adding elements to the linked list
        object.add("A");
        object.add("B");
        object.addLast("C");
        object.addFirst("D");
        object.add(2, "E");
        object.add("F");
        object.add("G");
        System.out.println("Linked list : " + object);
  
        // Removing elements from the linked list
        object.remove("B");
        object.remove(3);
        object.removeFirst();
        object.removeLast();
        System.out.println("Linked list after deletion: " + object);
  
        // Finding elements in the linked list
        boolean status = object.contains("E");
  
        if(status)
            System.out.println("List contains the element 'E' ");
        else
            System.out.println("List doesn't contain the element 'E'");
  
        // Number of elements in the linked list
        int size = object.size();
        System.out.println("Size of linked list = " + size);
  
        // Get and set elements from linked list
        Object element = object.get(2);
        System.out.println("Element returned by get() : " + element);
        object.set(2, "Y");
        System.out.println("Linked list after change : " + object);
    }
}

Output:

Linked list : [D, A, E, B, C, F, G]
Linked list after deletion: [A, E, F]
List contains the element 'E' 
Size of linked list = 3
Element returned by get() : F
Linked list after change : [A, E, Y]

Methods for Java LinkedList:

  1. int size(): It returns the number of elements in this list.
  2. void clear(): It removes all of the elements from the list.
  3. Object clone(): It is used to make the copy of an existing linked list.
  4. Object set(int index, Object element): It is used to replace an existing element in the list with a new element.
  5. boolean contains(Object element): It returns true if the element is present in the list.
  6. boolean add(Object element): It appends the element to the end of the list.
  7. void add(int index, Object element): It inserts the element at the position ‘index’ in the list.
  8. boolean addAll(Collection C): It appends a collection to a Linked List.
  9. boolean addAll(int index, Collection C): It appends a collection to a linked list at a specified position.
  10. void addFirst(Object element): It inserts the element at the beginning of the list.
  11. void addLast(Object element): It appends the element at the end of the list.
  12. Object get(int index): It returns the element at the position ‘index’ in the list. It throws ‘IndexOutOfBoundsException’ if the index is out of range of the list.
  13. Object getFirst(): It returns the first element of the Linked List.
  14. Object getLast(): It returns the last element of the Linked List.
  15. int indexOf(Object element): If element is found, it returns the index of the first occurrence of the element. Else, it returns -1.
  16. int lastIndexOf(Object element): If element is found, it returns the index of the last occurrence of the element. Else, it returns -1.
  17. Object remove(): It is used to remove and return the element from the head of the list.
  18. Object remove(int index): It removes the element at the position ‘index’ in this list. It throws ‘NoSuchElementException’ if the list is empty.
  19. boolean remove(Object O): It is used to remove a particular element from the linked list and returns a boolean value.
  20. Object removeLast(): It is used to remove and return the last element of the Linked List.

 
This article is contributed by Mehak Narang.

 
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



My Personal Notes arrow_drop_up

Improved By : Chinmoy Lenka