How to use Linked List In Java Without Using Collection Class

This tutorial will show you how to use Linked List in Java without using Collection class with an easy example.

A Collection class consists mainly of static methods that return or works with collections. It isĀ  invoked by the following statement at the beginning of the program:

public class Collections
extends Object

So let us see how can we create a linked list using collection class in java. Creating linked lists with collection class has the following advantage that you’ll be able to use predefined functions.

The following program suggests the use of the collection class to create a linked list. We are using this program to clearly identify the differences between the linked list using collection and linked list without collection class.

Program to use Linked List with “collection”:

import java.util.*; 
class Link 
  public static void main(String args[]) 
    // We are creating an object of class linked list 
    LinkedList<String> linkobj = new LinkedList<String>();  //This is a predefined statement if we want ot declae a linked list 

    // Adding elements to the object we have just created  
    linkobj.add("A"); // We are using predefined functions here provided by the collection class
      	System.out.println("Linked list : " + linkobj);  

The output of the above program:

How to use Linked List In Java Without Using Collection Class


As we can see if we use the collection class it becomes much easier to use the linked list as it already has inbuilt functions to perform operations like add, remove, count, check and replace its elements.

Now we shall see how to use a linked list without using collection class.

Program to use the linked list without collection class:

public class linkedlist
    public static class Node         //We are defining a node  here
        int data;       //data of node here
        Node next;      //next refers to the address of the next node
    public static void main(String args[])
        Node newnode1=new Node();    //here we are making objects of class Node
        Node newnode2=new Node();    //basically these are nodes of first linkedlist
        Node newnode3=new Node();    
        Node newnode4=new Node();   ;;;;
         ;       //here we are linking the previous node to the next node of the list;;;

        //Now we will print the linked list

       for (int i=1;i<=4;i++)
            System.out.print(" ");
  ;       //we are assigning the next address of the linked list to the current address 

The output of the above program is:

How to use Linked List In Java Without Using Collection Class


So we can see using collection class is way easier than normally declaring a linked list.

Also read:

One response to “How to use Linked List In Java Without Using Collection Class”

  1. Hari says:

    Not a correct solution. Works only when you know what the length of the linked list is. In general a raw linked list implementation is one where you don’t know the length.

    So, you will have to use something like this. You can assume you are given access to the head node of the Linked List, else

    //It’s always good to preserve the head node and not use it for any operations. Unless you are sorting or reversing a linked list in which case you need to update the head node.

    Node head, curr;

    while(!=null) {
    System.out.print(” “);;

Leave a Reply

Your email address will not be published. Required fields are marked *