dsa

Java LinkedList

In this tutorial, we will learn about the LinkedList, its methods and how to implement it in Java.

LinkedList class implements Deque interface and itprovides us the functionality of LinkedList data structures 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.

Constructors in the LinkedList

To use methods of an Vector class we have to create objects of Vector which can be done by the constructors of the LinkedList.There are four constructors present in Vector :

  1. LinkedList(): This constructor is used to create an empty linked list. It can be created as:
    LinkedList ll = new LinkedList():;
  2. LinkedList(Collection C): This constructor is used to create an ordered list which contains all the elements of a specified collection, as returned by the collection’s iterator.
    LinkedList ll = new LinkedList(C);

Methods of LinkedList

MethodsDescription
add(int index, E element)This method inserts the given element at the given position in the list.
add(E e)This method appends the given element to the end of this list.
addAll(int index, Collection c)This method inserts all of the elements in the given collection into this list, starting at the specified position.
addAll(Collection c)This method Appends all of the elements in the given collection to the end of this list, in the order that they are returned by the given collection’s iterator.
addFirst(E e)This method inserts the given element at the beginning of this list.
addLast(E e)This method appends the given 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 given 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 given element in this list, or -1 if this list does not contain the element.
lastIndexOf(Object o)This method returns the index of the last occurrence of the given element in this list, or -1 if this list does not contain the element.
listIterator(int index)This method returns a list-iterator of the elements in this list (in proper sequence), starting at the given position in the list.
offer(E e)This method Adds the given element as the tail (last element) of this list.
offerFirst(E e)This method Inserts the given element at the front of this list.
offerLast(E e)This method Inserts the given element at the end of this list.
peek()This method retrieves, but does not remove, the head (first element) of this list.
peekFirst()This method retrieves, but does not remove, the first element of this list, or returns null if this list is empty.
peekLast()This method retrieves, but does not remove, the last element of this list, or returns null if this list is empty.
poll()This method retrieves and removes the head (first element) of this list.
pollFirst()This method retrieves and removes the first element of this list, or returns null if this list is empty.
pollLast()This method retrieves and removes the last element of this list, or returns null if this list is empty.
pop()This method Pops an element from the stack represented by this list.
push(E e)This method Pushes an element onto the stack represented by this list.
remove()This method retrieves and removes the head (first element) of this list.
remove(int index)This method removes the element at the specified position in this list.
remove(Object o)This method removes the first occurrence of the specified element from this list, if it is present.
removeFirst()This method removes and returns the first element from this list.
removeFirstOccurrence(Object o)This method removes the first occurrence of the specified element in this list (when traversing the list from head to tail).
removeLast()This method removes and returns the last element from this list.
removeLastOccurrence(Object o)This method removes the last occurrence of the specified element in this list (when traversing the list from head to tail).
set(int index, E element)This method replaces the element at the specified position in this list with the specified element.
size()This method returns the number of elements in this list.
spliterator()This method Creates a late-binding and fail-fast Spliterator over the elements in this list.
toArray()This method returns an array containing all of the elements in this list in proper sequence (from first to last element).
toArray(T[] a)This method returns an array containing all of the elements in this list in proper sequence (from first to last element).
toString()This method returns a String containing all of the elements in this list in proper sequence (from first to last element), each element is separated by commas and the String is enclosed in square brackets.

Example 1


import java.util.*; 

public class CodeMistic 
{ 

	public static void main(String args[]) 
	{ 
		// Creating object of the class linked list 
		LinkedList<String> ll = new LinkedList(); 

		// Adding elements to the linked list 
		ll.add("A"); 
		ll.add("B"); 
		ll.addLast("C"); 
		ll.addFirst("D"); 
		ll.add(2, "E"); 

		System.out.println(ll); 

		ll.remove("B"); 
		ll.remove(3); 
		ll.removeFirst(); 
		ll.removeLast(); 

		System.out.println(ll); 
	} 
} 


Output:

[D, A, E, B, C]                                                                                                                
[A] 

Example 2


// Java program to add elements to a LinkedList 
	
import java.util.*; 
	
public class CodeMistic 
{ 
	public static void main(String args[]) 
	{ 
		LinkedList<String> ll = new LinkedList<>(); 
	
		ll.add("You"); 
		ll.add("Using"); 
		ll.add(1, "Are"); 
		ll.add(3, "CodeMistic.");
		System.out.println(ll); 
	} 
} 


Output:

[You, Are, Using, CodeMistic.]

Example 3


// Java program to change elements in a LinkedList 
	
import java.util.*; 
	
public class CodeMistic 
{
	public static void main(String args[]) 
	{ 
		LinkedList<String> ll = new LinkedList<>(); 
	
		ll.add("I"); 
		ll.add("CodeMistic"); 
		ll.add(1, "Like"); 
	
		System.out.println("Initial LinkedList " + ll); 
	
		ll.set(1, "Love"); 
	
		System.out.println("Updated LinkedList " + ll); 
	} 
} 



Output:

Initial LinkedList [I, Like, CodeMistic]                                                                                       
Updated LinkedList [I, Love, CodeMistic]

Example 4


// Java program to remove elements in a LinkedList 
	
import java.util.*; 
	
public class CodeMistic 
{
	public static void main(String args[]) 
	{ 
		LinkedList<String> ll = new LinkedList<>(); 
	
		ll.add("I"); 
		ll.add("CodeMistic"); 
		ll.add(1, "Love"); 
	
		System.out.println( 
			"Initial LinkedList " + ll); 
	
		ll.remove(0); 
	
		System.out.println( 
			"After the Index Removal " + ll); 
	
		ll.remove("Love"); 
	
		System.out.println( 
			"After the Object Removal " + ll); 
	} 
} 


Output:

Initial LinkedList [I, Love, CodeMistic]                                                                                       
After the Index Removal [Love, CodeMistic]                                                                                     
After the Object Removal [CodeMistic]

Example 5


// Java program to iterate the elements in an LinkedList 
	
import java.util.*; 
	
public class CodeMistic 
{
	public static void main(String args[]) 
	{ 
		LinkedList<String> ll = new LinkedList<>(); 
	
		ll.add("I"); 
		ll.add("Using");
		ll.add("CodeMistic") 
		ll.add(1, "Love"); 
	
		// Using the Get method 
		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:

I Love Using CodeMistic                                                                                                        
I Love Using CodeMistic