Linkedlist

Linked List is a part of the Collection framework present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure 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.

How Does LinkedList work Internally?

  • Since a LinkedList acts as a dynamic array and we do not have to specify the size while creating it, the size of the list automatically increases when we dynamically add and remove items.

  • And also, the elements are not stored in a continuous fashion. Therefore, there is no need to increase the size. Internally, the LinkedList is implemented using the doubly linked list data structure.

  • The main difference between a normal linked list and a doubly LinkedList is that a doubly linked list contains an extra pointer, typically called the previous pointer, together with the next pointer and data which are there in the singly linked list.

Constructors in the LinkedList

LinkedList()

This constructor is used to create an empty linked list. If we wish to create an empty LinkedList with the name ll, then, it can be created as:

LinkedList ll = new LinkedList(); 

LinkedList(Collection C)

This constructor is used to create an ordered list that contains all the elements of a specified collection, as returned by the collection’s iterator. If we wish to create a LinkedList with the name ll, then, it can be created as:

LinkedList ll = new LinkedList(C);

Methods in LinkedList

MethodDescription
add(int index, E element)This method Inserts the specified element at the specified position in this list.
add(E e)This method Appends the specified element to the end of this list.
addAll(int index, Collection c)This method Inserts all of the elements in the specified collection into this list, starting at the specified position.
addAll(Collection c)This method Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s iterator.
addFirst(E e)This method Inserts the specified element at the beginning of this list.
addLast(E e)This method Appends the specified element to the end of this list.
clear()This method removes all of the elements from this list.
clone()This method returns a shallow copy of this LinkedList.
contains(Object o)This method returns true if this list contains the specified element.
descendingIterator()This method returns an iterator over the elements in this deque in reverse sequential order.
element()This method retrieves but does not remove, the head (first element) of this list.
get(int index)This method returns the element at the specified position in this list.
getFirst()This method returns the first element in this list.
getLast()This method returns the last element in this list.
indexOf(Object o)This method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element

Operations on LinkedList

Adding Elements

In order to add an element to an ArrayList, we can use the add() method. This method is overloaded to perform multiple operations based on different parameters. They are:

  • add(Object): This method is used to add an element at the end of the LinkedList.

  • add(int index, Object): This method is used to add an element at a specific index in the LinkedList.

// Java program to add elements
// to a LinkedList

import java.util.*;

public class TFT {

    public static void main(String args[])
    {
        LinkedList<String> ll = new LinkedList<>();

        ll.add("Tit");
        ll.add("Tat");
        ll.add(1, "For");

        System.out.println(ll);
    }
}

Output

[Tit, For, Tat]

Changing Elements

After adding the elements, if we wish to change the element, it can be done using the set() method. Since a LinkedList is indexed, the element which we wish to change is referenced by the index of the element. Therefore, this method takes an index and the updated element which needs to be inserted at that index.

import java.util.*;

public class TFT {

    public static void main(String args[])
    {
        LinkedList<String> ll = new LinkedList<>();

        ll.add("Tit");
        ll.add("Tat");
        ll.add(1, "Tit");

        System.out.println("Initial LinkedList " + ll);

        ll.set(1, "For");

        System.out.println("Updated LinkedList " + ll);
    }
}

Output

Initial LinkedList [Tit, Tit, Tat]
Updated LinkedList [Tit, For, Tat]

Removing Elements

In order to remove an element from a LinkedList, we can use the remove() method. This method is overloaded to perform multiple operations based on different parameters. They are:

  • remove(Object): This method is used to simply remove an object from the LinkedList. If there are multiple such objects, then the first occurrence of the object is removed.

  • remove(int index): Since a LinkedList is indexed, this method takes an integer value which simply removes the element present at that specific index in the LinkedList. After removing the element and the indices of elements are updated so do the object of LinkedList is updated giving a new List after the deletion of element/s.

// Java program to remove elements
// in a LinkedList

import java.util.*;

public class TFT {

    public static void main(String args[])
    {
        LinkedList<String> ll = new LinkedList<>();

        ll.add("Tit");
        ll.add("Tat");
        ll.add(1, "For");

        System.out.println(
                "Initial LinkedList " + ll);

        ll.remove(1);

        System.out.println(
                "After the Index Removal " + ll);

        ll.remove("Tit");

        System.out.println(
                "After the Object Removal " + ll);
    }
}

Output

Initial LinkedList [Tit, For, Tat]
After the Index Removal [Tit, Tat]
After the Object Removal [Tat]

Iterating the LinkedList

There are multiple ways to iterate through LinkedList. The most famous ways are by using the basic for loop in combination with a get() method to get the element at a specific index and the advanced for-loop.

mport java.util.*;

public class TFT {

    public static void main(String args[])
    {
        LinkedList<String> ll
                = new LinkedList<>();

        ll.add("Tit");
        ll.add("Tat");
        ll.add(1, "For");

        // Using the Get method and the
        // for loop
        for (int i = 0; i < ll.size(); i++) {

            System.out.print(ll.get(i) + " ");
        }

        System.out.println();

        // Using the for each loop
        for (String str : ll)
            System.out.print(str + " ");
    }
}

Output

Tit For Tat 
Tit For Tat
core java programming linkedlist list collections data-structures

Subscribe For More Content