LinkedList is commonly used collection is java to store list of objects. It follows Doubly-linked list data structure.

We have seen the List interface and ArrayList in java. So we will also see what LinkedList is in java and how to create LinkedList and perform operations.

LinkedList

  1. LinkedList extends AbstractSequentialList class and implements the List and Deque interface. 
  2. The elements in the LinkedList are linked by using pointers and addresses. 
  3. Each element in the LinkedList is known as Node. 
  4. Internally LinkedList is implemented by using a doubly-linked list data structure.

Following is the graphical representation of LinkedList:

A node is an object that stores a reference to an element and node. The left node is used to store the reference to the previous element and node. The right node is used to store the reference to the next element and node

LinkedList has 2 constructors to create an object:

  • LinkedList list = new LinkedList();

This will create an empty list.

  • LinkedList list = new LinkedList(oldList);

This will create an object by taking a collection object as a parameter and copying all the values of oldList into a new list created.

Let’s see an example below to create a LinkedList using the above 2 constructors.

In the below example we created the first list1 using the first type and then list2 passing list1 as a parameter.

Java Code
import java.util.LinkedList;
class Test {
public static void main(String[] args) {
LinkedList list1 = new LinkedList<>();
list1.add(“ABCD”);
list1.add(“EFGH”);
list1.add(“IJKL”);
System.out.println(list1);

LinkedList list2 = new LinkedList<>(list1);
list2.add(“KAI”);
list2.add(“DOH”);
System.out.println(list2);
}
}
Output:
[ABCD, EFGH, IJKL] [ABCD, EFGH, IJKL, KAI, DOH]

LinkedList also has some methods same as follows:

  1. add(E e):
  2. add(int index, E element)
  3. addAll(Collection<? extends E> c)
  4. addAll(int index, Collection<? extends E> c)

These methods are used for adding elements or another list into another list. With index position given that element/list will be added at that index position else, it will be added at the end (appended) of the list. An example for add is as below. 

  1. addFirst(E e): This is used to add the element at the beginning of the list.
  2. addLast(E e): This is used to add the element at the end of the list.
Java Code
import java.util.LinkedList;
class Test {
public static void main(String[] args) {
LinkedList list = new LinkedList<>();
list.add(“ABCD”); // add element at 0th index
list.add(0, “EFGH”); // add element at 0th index and shift ABCD to 1st index
list.add(“IJKL”); //add element at index 2

// Creating childList
LinkedList childList = new LinkedList<>();
childList.add(“KAI”);
childList.add(“DOH”);

list.addAll(childList); // append the child list from end index 3.
System.out.println(list);

list.addAll(1, childList); //adds the child list elements from index 1.
System.out.println(list);

list.addFirst(“BAEK”); // adds the value at beginning
list.addLast(“CHAN”); // adds the element at the end

System.out.println(“Final List: “+ list);
}
}
Output:
[EFGH, ABCD, IJKL, KAI, DOH] [EFGH, KAI, DOH, ABCD, IJKL, KAI, DOH] Final List: [BAEK, EFGH, KAI, DOH, ABCD, IJKL, KAI, DOH, CHAN]
  1. clone(): clones the copy of the LinkedList.
  2. contains(Object o): checks if the list contains a specific element.
  3. indexOf(Object o): Returns the index of the first occurrence of the specified element else returns -1 if not present in the list.
  4. lastIndexOf(Object o): Returns the index of the last occurrence of the specified element else returns -1 if not present in the list.
  5. size(): Returns the size of the list
Java Code
import java.util.LinkedList;
class Test {
public static void main(String[] args) {
LinkedList list = new LinkedList<>();
list.add(“IJKL”);
list.add(“ABCD”);
list.add(0, “EFGH”);
list.add(“IJKL”);

System.out.println(“Original List: ” + list);
// Clonning the list into new list
LinkedList newList = (LinkedList)list
.clone();
System.out.println(“New List: ” + newList);

System.out.println(“Contains IJKL: ” + list.contains(“IJKL”));
System.out.println(“index of IJKL: ” + list.indexOf(“IJKL”));
System.out.println(“last index of IJKL: ” + list.lastIndexOf(“IJKL”));
System.out.println(“size of list: ” + list.size());
}
}
Output:
Original List: [EFGH, IJKL, ABCD, IJKL] New List: [EFGH, IJKL, ABCD, IJKL] Contains IJKL: true
index of IJKL: 1
last index of IJKL: 3
size of list: 4
  1. descendingIterator(): Returns an iterator in reverse order.
  2. listIterator(int index): This returns the list iterator which is also a fail-fast iterator. 
Java Code
import java.util.LinkedList;
import java.util.Iterator;
import java.util.ListIterator;
class Test {
public static void main(String[] args) {
LinkedList list = new LinkedList<>();
list.add(“IJKL”);
list.add(“ABCD”);
list.add(“EFGH”);

System.out.println(“Original list is: ” + list);
// iterating in reverse order
System.out.print(“List in reverse Order: “);
Iterator itr = list.descendingIterator();
while(itr.hasNext()){
System.out.print(” ” + itr.next());
}
System.out.println();
// iterating using list iterator we can also specify the starting index
System.out.print(“List in using listIterator: “);
ListIterator itr2 = list.listIterator();
while(itr2.hasNext()){
System.out.print(” ” + itr2.next());
}
}
}
Output:
Original list is: [IJKL, ABCD, EFGH] List in reverse Order: EFGH ABCD IJKL
List in using listIterator: IJKL ABCD EFGH
  1. remove(): removes the first element from the list.
  2. remove(int index): Removes the element at the specified position in the list.
  3. remove(Object o): Removes the first occurrence of the specified element from the list, if it’s present.
  4. removeFirst(): Removes and returns the first element from the list.
  5. removeFirstOccurrence(Object o): Removes the first occurrence of the specified element in this list.
  6. removeLast(): Removes and returns the last element from the list.
  7. removeLastOccurrence(Object o): Removes the last occurrence of the specified element in the list.
  8. clear(): Deletes all of the elements from the list.
Java Code
import java.util.LinkedList;
class Test {
public static void main(String[] args) {
LinkedList list = new LinkedList<>();
list.add(“IJKL”);
list.add(“CBXS”);
list.add(“DOH”);
list.add(“KAI”);
list.add(“BAEK”);
list.add(“EFGH”);
list.add(“CHAN”);
list.add(“JKV”);
list.add(“KIMJ”);
list.add(“BAEK”);
list.add(“CHAN”);
System.out.println(“Original List: ” + list);
System.out.println(“remove(): ” + list.remove() + “, updated list : ” + list);// returns the element and remove from index 0
System.out.println(“remove(2): ” + list.remove(2) + “, updated list : ” + list);// returns the element and remove from index 2
System.out.println(“remove(EFGH): ” + list.remove(“EFGH”) + “, updated list : ” + list);// removes the EFGH

System.out.println(“removeFirst(): ” + list.removeFirst() + “, updated list : ” + list);
System.out.println(“removeFirstOccurrence(CHAN): ” + list.removeFirstOccurrence(“CHAN”) + “, updated list : ” + list);
System.out.println(“removeLast(): ” + list.removeLast() + “, updated list : ” + list);
System.out.println(“removeLastOccurrence(BAEK): ” + list.removeLastOccurrence(“BAEK”) + “, updated list : ” + list);

list.clear(); //clearing list removing all elements
System.out.println(“Final list: ” + list);
}
}
Output:
Original List: [IJKL, CBXS, DOH, KAI, BAEK, EFGH, CHAN, JKV, KIMJ, BAEK, CHAN] remove(): IJKL, updated list : [CBXS, DOH, KAI, BAEK, EFGH, CHAN, JKV, KIMJ, BAEK, CHAN] remove(2): KAI, updated list : [CBXS, DOH, BAEK, EFGH, CHAN, JKV, KIMJ, BAEK, CHAN] remove(EFGH): true, updated list : [CBXS, DOH, BAEK, CHAN, JKV, KIMJ, BAEK, CHAN] removeFirst(): CBXS, updated list : [DOH, BAEK, CHAN, JKV, KIMJ, BAEK, CHAN] removeFirstOccurrence(CHAN): true, updated list : [DOH, BAEK, JKV, KIMJ, BAEK, CHAN] removeLast(): CHAN, updated list : [DOH, BAEK, JKV, KIMJ, BAEK] removeLastOccurrence(BAEK): true, updated list : [DOH, BAEK, JKV, KIMJ] Final list: []
  1. set(int index, E element): to set the element at specified index.
  2. get(int index):  to get the element from specified index position.
  3. getFirst(): to get the first element.
  4. getLast(): to get the last element.
Java Code
import java.util.LinkedList;
class Test {
public static void main(String[] args) {
LinkedList list = new LinkedList<>();
list.add(“CBXS”);
list.add(“DOH”);
list.add(“IJKL”);
list.add(“KAI”);

System.out.println(“Original List: ” + list);
System.out.println(“set(2, CHAN): ” + list.set(2, “CHAN”) + “, updated list : ” + list);
System.out.println(“get(3): ” + list.get(3));
System.out.println(“getFirst(): ” + list.getFirst());
System.out.println(“getLast(): ” + list.getLast());
}
}
Output:
Original List: [CBXS, DOH, IJKL, KAI] set(2, CHAN): IJKL, updated list : [CBXS, DOH, CHAN, KAI] get(3): KAI
getFirst(): CBXS
getLast(): KAI
  1. element(): Retrieves, but does not remove the first element.
  2. offer(E e): Adds the specified element at the end.
  3. offerFirst(E e): Inserts the specified element at first.
  4. offerLast(E e): Inserts the specified element at the end.
  5. peek(): Retrieves, but does not remove, the first element.
  6. peekFirst(): Retrieves, but does not remove, the first element or returns null if the list is empty.
  7. peekLast(): Retrieves, but does not remove, the last element or returns null if the list is empty.
  8. poll(): Retrieves and removes the first element.
  9. pollFirst(): Retrieves and removes the first element or returns null if the list is empty.
  10. pollLast(): Retrieves and removes the last element or returns null if the list is empty.
  11. pop(): Pops(Retrieves and removes) the first element from the stack represented by the list.
  12. push(E e): Pushes an element onto the stack represented by this list.
Java Code
import java.util.LinkedList;
class Test {
public static void main(String[] args) {
LinkedList list = new LinkedList<>();
list.add(“CBXS”);
list.add(“DOH”);
list.add(“IJKL”);
list.add(“KAI”);

System.out.println(“>> Original List ” + list);
System.out.println(“element ” + list.element());
System.out.println(“offer(CHAN): ” + list.offer(“CHAN”));
System.out.println(“offerFirst(CHEN): ” + list.offerFirst(“CHEN”));
System.out.println(“offerLast(KSOO): ” + list.offerLast(“KSOO”));
System.out.println(“>> Updated List ” + list);

System.out.println(“peek(): ” + list.peek());
System.out.println(“peekFirst(): ” + list.peekFirst());
System.out.println(“peekLast(): ” + list.peekLast());
System.out.println(“>> List after peek” + list);

System.out.println(“poll(): ” + list.poll());
System.out.println(“pollFirst(): ” + list.pollFirst());
System.out.println(“pollLast(): ” + list.pollLast());
System.out.println(“>> List after poll” + list);

System.out.println(“pop(): ” + list.pop());
System.out.println(“>> List after pop” + list);

list.push(“BAEK”);
System.out.println(“>> Final List” + list);
}
}
Output:
>> Original List [CBXS, DOH, IJKL, KAI] element CBXS
offer(CHAN): true
offerFirst(CHEN): true
offerLast(KSOO): true
>> Updated List [CHEN, CBXS, DOH, IJKL, KAI, CHAN, KSOO] peek(): CHEN
peekFirst(): CHEN
peekLast(): KSOO
>> List after peek[CHEN, CBXS, DOH, IJKL, KAI, CHAN, KSOO] poll(): CHEN
pollFirst(): CBXS
pollLast(): KSOO
>> List after poll[DOH, IJKL, KAI, CHAN] pop(): DOH
>> List after pop[IJKL, KAI, CHAN] >> Final List[BAEK, IJKL, KAI, CHAN]
  1. toArray(): This will return the new array object with all the elements from first to last in the sequence.
  2. toArray(T[] a): This will return the new array object where the runtime type of the returned array is that of the specified array.
Java Code
import java.util.LinkedList;
class Test {
public static void main(String[] args) {
LinkedList list = new LinkedList<>();
list.add(“CBXS”);
list.add(“DOH”);
list.add(“IJKL”);
list.add(“KAI”);

// using toArray()
Object arr[] = list.toArray();
System.out.print(“Array converted: “);
for(Object o : arr){
System.out.print(” ” + o);
}

// using toArray(T[] a)
String strArr[] = new String[list.size()];
strArr = list.toArray(strArr);
System.out.print(“\nString Array converted: “);
for(String o : strArr){
System.out.print(” ” + o);
}
}
}
Output:
Array converted: CBXS DOH IJKL
String Array converted: CBXS DOH IJKL KAI

-A blog by Shwetali Khambe

Related Posts