a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes which together represent a sequence. In its most basic form, each node contains: data and a reference (in other words, a link) to the next node in the sequence. This structure allows for efficient insertion or removal of elements from any position in the sequence during iteration.

Generally a linked list contains ā€˜n’ number of nodes. A node contains two parts – data and link.

In simple words, a linked list is a linear data structure where an element does not stored in a contiguous way. See below image…

Image credit: https://commons.wikimedia.org/wiki/File:C_language_linked_list.png

We will do several operations over linked list using collections framework in java.

  1. Write a code, to construct a singly Integer linked list.
import java.util.*;
 
class linkedList1{
                    public static void main(String[] args) {
                                        LinkedList <Integer> lst = new LinkedList<>();
                                        lst.add(0,10);
                                        lst.add(1,40);
                                        lst.add(2,-60);
                                        lst.add(3,78);
 
                                        System.out.println(“linked list is: “);       
                                        System.out.println(lst);
                                       
                    }
}
output

2. Write a code, which will perform deletion on Integer linked list.

import java.util.*;
 
class linkedList2{
                    public static void main(String[] args) {
                                        LinkedList <Integer> lst = new LinkedList<>();
                                        lst.add(0,10);
                                        lst.add(1,40);
                                        lst.add(2,-60);
                                        lst.add(3,78);
 
                                        System.out.println(“linked list is: “);       
                                        System.out.println(lst);
                                       
        // remove() method removes(delete) element from linked list
        lst.remove(1);
        System.out.println(“after deletion linked list: “);              
                                        System.out.println(lst);
                                       
        lst.remove(2);
        System.out.println(“after deletion linked list: “);              
                                        System.out.println(lst);
                                       
                    }
}
output

3. Write a code, which will check an element is present or not in the Integer linked list.

import java.util.*;
 
class linkedlist3{
                    public static void main(String[] args) {
                                        LinkedList <Integer> lst = new LinkedList<>();
                                        lst.add(0,10);
                                        lst.add(1,40);
                                        lst.add(2,-60);
                                        lst.add(3,78);
 
                                        System.out.println(“linked list is: “);       
                                        System.out.println(lst);
                                       
        Scanner sc = new Scanner(System.in);
        System.out.println(“enter element to found: “);
        int n = sc.nextInt();
 
        if(lst.contains(n)){
            System.out.println(“element found!!”);
        }
                                       
                    }
}
output

4. Write a code, which will finds maximum and minimum element in a Integer linked list.

import java.util.*;
 
class linkedlist4{
               public static void main(String[] args) {
                              LinkedList <Integer> lst = new LinkedList<>();
                              lst.add(0,10);
                              lst.add(1,40);
                              lst.add(2,-60);
                              lst.add(3,78);
 
                              System.out.println(“linked list is: “);            
                              System.out.println(lst);
                             
       System.out.println(“maximum element is “+Collections.max(lst));
        System.out.println(“maximum element is “+Collections.min(lst));
      }
}
output

5. Write a code, which will perform addition of all elements which present in String Linked List.

import java.util.*;
 
class strLL{
    public static void main(String[] args){
        LinkedList <String> lst = new LinkedList<>();
        lst.add(“java”);
        lst.add(“python”);
        lst.add(“php”);
        lst.add(“css”);
        lst.add(“html”);
       
        System.out.println(“linked list -> “+lst);
        String n,sum=””;
        for(int i=0;i<lst.size();i++){
            // get() method helps accessing elements using index
            n = lst.get(i);
            sum = sum+n;
        }
        System.out.println(“total strung is -> “+sum);
    }
}
output

Note: linkedlist also manages memory same like arraylist

let’s understand through below code…

import java.util.*;
 
class strLL1{
    public static void main(String[] args){
        LinkedList <String> lst = new LinkedList<>();
        lst.add(“java”);
        lst.add(“python”);
        lst.add(“php”);
        lst.add(“css”);
        lst.add(“html”);
       
        System.out.println(“linked list -> “+lst);
        System.out.println(“linked list size is “+lst.size());
 
        // removing elements from linked list by it’s name
        lst.remove(“php”);
        System.out.println(“linked list -> “+lst);
        System.out.println(“linked list size is “+lst.size());
 
        // removing elements from linked list by it’s index
        lst.remove(2);
         System.out.println(“linked list -> “+lst);
        System.out.println(“linked list size is “+lst.size());
 
    }
}
output

6. Write a code, to add new element in linked list.

import java.util.*;
 
class LLupdate{
   public static void main(String[] args){
    LinkedList <Integer> fll = new LinkedList<>();
    fll.add(10);
    fll.add(10);
    fll.add(10);
    fll.add(10);
    fll.add(10);
   
    System.out.println(“linked list is -> “+fll);
 
    // adding new element into index 0 ->35
    fll.add(0,35);
    System.out.println(“linked list is -> “+fll);
 
     // adding new element into index 0 ->67
    fll.add(2,67);
    System.out.println(“linked list is -> “+fll);
 }
}
output