Linked List in Java

Article by: Manish Methani

Last Updated: October 24, 2021 at 2:04pm IST
LinkedList Java 7 min 11 sec read

LinkedList java is a linear data Structure used to store elements dynamically. No need to know the actual size. In case of arrays, you must know the size of an array.

Following are the constructors supported by the LinkedList class.

Sr.No. Constructor & Description
1

LinkedList( )

This constructor builds an empty linked list.

2

LinkedList(Collection c)

This constructor builds a linked list that is initialized with the elements of the collection c.

Apart from the methods inherited from its parent classes, LinkedList defines following methods −

Sr.No. Method & Description
1

void add(int index, Object element)

Inserts the specified element at the specified position index in this list. Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 || index > size()).

2

boolean add(Object o)

Appends the specified element to the end of this list.

3

boolean addAll(Collection c)

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. Throws NullPointerException if the specified collection is null.

4

boolean addAll(int index, Collection c)

Inserts all of the elements in the specified collection into this list, starting at the specified position. Throws NullPointerException if the specified collection is null.

5

void addFirst(Object o)

Inserts the given element at the beginning of this list.

6

void addLast(Object o)

Appends the given element to the end of this list.

7

void clear()

Removes all of the elements from this list.

8

Object clone()

Returns a shallow copy of this LinkedList.

9

boolean contains(Object o)

Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

10

Object get(int index)

Returns the element at the specified position in this list. Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 || index >= size()).

11

Object getFirst()

Returns the first element in this list. Throws NoSuchElementException if this list is empty.

12

Object getLast()

Returns the last element in this list. Throws NoSuchElementException if this list is empty.

13

int indexOf(Object o)

Returns the index in this list of the first occurrence of the specified element, or -1 if the list does not contain this element.

14

int lastIndexOf(Object o)

Returns the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.

15

ListIterator listIterator(int index)

Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list. Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 || index >= size()).

16

Object remove(int index)

Removes the element at the specified position in this list. Throws NoSuchElementException if this list is empty.

17

boolean remove(Object o)

Removes the first occurrence of the specified element in this list. Throws NoSuchElementException if this list is empty. Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 || index >= size()).

18

Object removeFirst()

Removes and returns the first element from this list. Throws NoSuchElementException if this list is empty.

19

Object removeLast()

Removes and returns the last element from this list. Throws NoSuchElementException if this list is empty.

20

Object set(int index, Object element)

Replaces the element at the specified position in this list with the specified element. Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 || index >= size()).

21

int size()

Returns the number of elements in this list.

22

Object[] toArray()

Returns an array containing all of the elements in this list in the correct order. Throws NullPointerException if the specified array is null.

23

Object[] toArray(Object[] a)

Returns an array containing all of the elements in this list in the correct order; the runtime type of the returned array is that of the specified array.

Example :-

In this example you learn how to create an empty LinkedList, how to use .clear() method , .subList() method, ListIteartor() method , .hasPrevious() method.

import java.util.*;
public class JavaLinkedListDemo {
  public static void main(String[] args)
  {
  String[] things = {"eggs" , "hats" , "laser" , "pie"};
  
 // Create an empty LinkedList
  LinkedList list1 =  new LinkedList();
  
  // Add array items to list1
  for(String x: things)
  {
    list1.add(x);
  }

  // Another LinkedList
  String[] morethings = {"eggs" , "hats"};
  LinkedList list2 =  new LinkedList();

  //add array items to list2
  for(String y: morethings)
  {
   list2.add(y);
  }
  
  // Linked List method which adds all the elements of list 
  list1.addAll(list2);
  list2 = null;

  //prints all the items in the list1
  printItems(list1);

  //Remove items from 2 to 4 in list1
  removeItemss(list1,2,4);


  //print the updated list items in the list1
  printItems(list1);
 
  //reverse all the elements in list1
  reverseTheList(list1);
  }
  
//Print method to print elements in the list
private static void printItems(List list) 
{
  for(String s: list)
  {
    System.out.printf("%s ", s);
   
  }
  System.out.println();
}

//Remove method to remove elements in the list
private static void removeItemss(List list,int from, int to)
{
  list.subList(from, to).clear();
  System.out.println();
}

//Reverse the List
private static void reverseTheList(List list)
{
    ListIterator iter = list.listIterator(list.size());
    while(iter.hasPrevious())
    {
      System.out.printf("%s ", iter.previous());
    }
}

}

Output

eggs hats laser pie eggs hats 

eggs hats eggs hats 
hats eggs hats eggs 

Comments itself are self-explanatory. In this example, we created two LinkedList and combined them using .addAll() method. Then cleared the elements in list1 from index 2 to 4 using .clear() method. Finally, we reversed the final elements in list 1 using ListIterator

Watch Video Tutorials at Codzify YouTube Channel:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com