Java Online Compiler

LinkedList

  • It is a predefined class in java.util package. I is used to store elements in the form of nodes.
  • In java Language bydefault double linked list will be represented by linked class.
  • In the double linked list last node contains 3 blocks outof these address block1 holds address of the previous node address, block2 holds address of next node address and datablock holds elements.
  • Linkedlist also accepts any type of hetrogeneous elements by supporting duplication.
LinkedList<wrapperclass> objref=new LinkedList<wrapperclass>();         
                                    
Linkedlist contains fllowing methods.

LinkedList Add Method:
It is used to new element in the new node.

import java.util.*;
class LinkedListAdd
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
al.add(50);
System.out.println("The List Items Are "+al);
}
}

class Main
{
public static void main(String aargs[])
{
LinkedListAdd d=new LinkedListAdd();
d.list();
}
}

LinkedList AddFirst Method:
It is used to add the element before first node.

import java.util.*;
class LinkedListAddFirst
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
al.addFirst(500);
System.out.println("The List Items Are "+al);
}
}

class Main
{
public static void main(String aargs[])
{
LinkedListAddFirst d=new LinkedListAddFirst();
d.list();
}
}

LinkedList AddIndex Method
It is used to add the element based on the index value(insertion of the node)

import java.util.*;
class LinkedListAddIndex
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
System.out.println("The List Items Are "+al);
al.add(1,1000);
System.out.println(al);
}
}

class Main
{
public static void main(String aargs[])
{
LinkedListAddIndex d=new LinkedListAddIndex();
d.list();
}
}

LinkedList AddLast Method
It is used to add the new element after the last node.
Note: by default add method access the last method.

import java.util.*;
class LinkedListAddLast
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.addLast(500);
al.add(30);
al.add(40);
System.out.println("The List Items Are "+al);
}
}

class Main
{
public static void main(String aargs[])
{
LinkedListAddLast d=new LinkedListAddLast();
d.list();
}
}

LinkedList Clear Method
It is used ti clear the all elements from all nodes.

import java.util.*;
class LinkedListClear
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
System.out.println("The List Items Are "+al);
al.clear();
System.out.println(al);
}
}

class Main
{
public static void main(String aargs[])
{
LinkedListClear d=new LinkedListClear();
d.list();
}
}

LinkedList GetFirst Method
It is used to get the first node element.

import java.util.*;
class LinkedListGetFirst
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
System.out.println("The List Items Are "+al);

System.out.println("The First Item is "+al.getFirst());
}
}

class Main
{
public static void main(String aargs[])
{
LinkedListGetFirst d=new LinkedListGetFirst();
d.list();
}
}

LinkedList GetIndex Method
It is used to get element based on the index value.

import java.util.*;
class LinkedListGetIndex
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
System.out.println("The List Items Are "+al);

System.out.println(al.get(3));
}
}

class Main
{
public static void main(String aargs[])
{
LinkedListGetIndex d=new LinkedListGetIndex();
d.list();
}
}

LinkedList GetLast Method
It is used to get the last node element.

import java.util.*;
class LinkedListGetLast
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
System.out.println("The List Items Are "+al);

System.out.println("The Last Item is "+al.getLast());
}
}

class Main
{
public static void main(String aargs[])
{
LinkedListGetLast d=new LinkedListGetLast();
d.list();
}
}

LinkedList RemoveFirst Method
It is used to remove first node element.

import java.util.*;
class LinkedListRemoveFirst
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
System.out.println("The List Items Are "+al);
al.removeFirst();
System.out.println(al);
}
}

class Main
{
public static void main(String aargs[])
{
LinkedListRemoveFirst d=new LinkedListRemoveFirst();
d.list();
}
}

LinkedList RemoveIndex Method :
It is used to remove the element based on the index value.

import java.util.*;
class LinkedListRemoveIndex
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
System.out.println("The List Items Are "+al);
al.remove(1);
System.out.println(al);
}
}

class Main
{
public static void main(String aargs[])
{
LinkedListRemoveIndex d=new LinkedListRemoveIndex();
d.list();
}
}

LinkedList RemoveLast Method :
It is used to remove the last node element

import java.util.*;
class LinkedListRemoveLast
{
void list()
{
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(20);
al.add(10);
al.add(30);
al.add(40);
System.out.println("The List Items Are "+al);
al.removeLast();
System.out.println(al);
}
}

class Main
{
public static void main(String aargs[])
{
LinkedListRemoveLast d=new LinkedListRemoveLast();
d.list();
}
}