LinkedList Class


  • The underlying data structure is double LinkedList
  • Insertion order is preserved.
  • Duplicate objects are allowed.
  • Heterogeneous objects are allowed.
  • null insertion is possible.
  • Implements Serializable and Cloneable interfaces but not RandomAccess.
  • If our frequent operation is insertion (or) deletion in the middle then LinkedList is the best choice.
  • If our frequent operation is retrieval operation then LinkedList is worst choice.

Usually we can use LinkedList to implement Stacks and Queues.

To provide support for this requirement LinkedList class defines the following 6 specific methods.

  • void addFirst(Object o);
  • void addLast(Object o);
  • Object getFirst();
  • Object getLast();
  • Object removeFirst();
  • Object removeLast();

We can apply these methods only on LinkedList object.

LinkedList l=new LinkedList();

Creates an empty LinkedList object.

LinkedList l=new LinkedList(Collection c);

To create an equivalent LinkedList object for the given collection.

package com.ashok.collections;

import java.util.LinkedList;

class MyLinkedList {
   public static void main(String[] args) {
      LinkedList list = new LinkedList();
      System.out.println(list);// [Ashok, 10, null, Ashok]
      list.set(0, "Software");
      System.out.println(list);// [Software, 10, null, Ashok]
      list.set(0, "Vinod");
      System.out.println(list);// [Vinod, 10, null, Ashok]
      System.out.println(list);// [Vinod, 10, null]
      System.out.println(list);// [Dillesh, Vinod, 30, null]
Inter conversion between collection objects
package com.ashok.collections.linkedlist;

import java.util.ArrayList;
import java.util.LinkedList;

public class MyCollectionsConversion {
   public static void main(String a[]) {
      ArrayList arrayList = new ArrayList();
      System.out.println("l1--->" + arrayList);
      LinkedList linkedList = new LinkedList(arrayList);
      linkedList.add(1, 5);
      linkedList.add(3, 5);
      linkedList.add(5, 15);
      System.out.println("l2--->" + linkedList);
      ArrayList arrayList1 = new ArrayList(linkedList);
      System.out.println("l3--->" + arrayList1);
l1--->[10, 20, 30]
l2--->[10, 5, 20, 5, 30, 15]
l3--->[10, 5, 20, 5, 30, 15]
LinkedList Class
Scroll to top