LinkedList in java

Let’s learn LinkedList in java.

LinkedList in java

Properties of LinkedList:

  • The underlying data structure of LinkedList is Double LinkedList.
  • Insertion order is preserved. That is order in which the elements is inserted is retrieved back in the same order.
  • Heterogeneous objects are allowed.
  • Null insertion is allowed.
  • LinkedList implements Serializable and Cloneable interfaces. Not RandomAccess Interface.
  • LinkedList is best if our frequent operation is insertion or deletion in the middle.
  • Do not use LinkedList if our frequent operation is retrieval from LinkedList.

Methods in LinkedList

Here’s the list of LinkedList specific methods.

void addFirst(); Inserts the specified element at the beginning of this list.

void addLast(); Appends the specified element to the end of this list.

Object getFirst(); Returns the first element in this list.

Object getLast(); Returns the last element in this list.

Object removeFirst(); Removes and returns the first element from this list.

Object removeLast(); Removes and returns the last element from this list.

Constructors in LinkedList

LinkedList ll = new LinkedList(); creates an empty LinkedList object.

LinkedList ll = new LinkedList(Collection c); creates an equivalent LinkedList object for the given Collection.

Now let us execute a java program on LinkedList.

import java.util.LinkedList;
class LinkedListDemo
    public static void main(String[] args)
        LinkedList ll = new LinkedList();
        ll.set(0, "sangeetha");
        ll.add(0, "virat");


[chethan, virat, sangeetha, 60, null]

Difference between ArrayList and LinkedList

ArrayList implements RandomAccess Interface.LinkedList do not implement RandomAccess Interface.
The underlying data structure for ArrayList is resizable/growable array.The underlying data structure for LinkedList is Double LinkedList.
ArrayList is the best alternative for retrieval operation.LinkedList is the best alternative for insertion and deletion of elements.
ArrayList is not recommended if our operation is insertion or deletion.LinkedList is not recommended if our operation is retrieval operation.