×

How to create a linked list in Java

Introduction: The linked listing is one type of linear statistics shaped like an array. Not like arrays, linked listing factors aren't stored in a contiguous place. The elements have linked the usage of suggestions as proven beneath. In the Java programming language, Linked List may represent a category and a Node as a separate elegance. The Linked List magnificence includes a reference of Node class kind.

Every element in a connected list is known as a node. It consists of 3 fields-

Prev - Shops and deals with the primary element within the listing. It's miles null for the primary detail.

Next - Store the address of the next part in the list. It's miles null for the closing detail. Facts - Stores the facts.

Algorithm of the linked list in java:

Create a category Node that has attributes –

  1. Facts and next. Next is a pointer to the next node.
  2. Create every other class that has attributes: head and tail.
  3. addNode() will upload a brand new node to the listing:
    1. Relate a new node.
    2. It first tests, whether the pinnacle is the same as null which means that the list is empty.
    3. If the list is empty, both head and tail will point to the newly added node.
    4. If the listing isn't empty, the brand new node can be added to the quit of the list such that the tail's subsequent will factor to the newly introduced node. This new node becomes the brand new tail of the listing.

Example 1: Here we give an example to create a linked list in Java.

import java.io.*;
public class LinkedList
{
Node head;
static class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
public static LinkedList insert (LinkedList list, int data)
{
Node new_node = new Node(data);
if (list.head == null){
list.head = new_node;
}
else
{
Node last = list.head;
While (last.next != null)
{
last = last.next;
}
last.next = new_node;
}
return list;
}
public static void printList (LinkedList list)
{
Node currNode = list.head;
System.out.println (“LinkedList: ”);
while (currNode != null)
{
System.out.println (currNode.data + “ ”);
currNode = currNode.next;
}
}
public static void main (String[] args)
LinkedList list = new LinkedList();
l = insert (l, 1);
l = insert (l, 2);
l= insert (l, 3);
l = insert (l, 4);
l = insert (l, 5);
l = insert (l, 6);
l = insert (l, 7);
l = insert (l, 8);
l = insert (l, 9);
l = insert (l, 10);
printList (l);
}
}

Output: We will compile the program and also run it.

LinkedList: 1 2 3 4 5 6 7 8 9 10

Example 2: Here we give an example to create a linked list in Java.

import java.io.*;
public class LinkedList
{
Node head;
static class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
public static LinkedList insert (LinkedList list, int data)
{
Node new_node = new Node(data);
if (list.head == null){
list.head = new_node;
}
else
{
Node last = list.head;
While (last.next != null)
{
last = last.next;
}
last.next = new_node;
}
return list;
}
public static void printList (LinkedList list)
{
Node currNode = list.head;
System.out.println (“LinkedList: ”);
while (currNode != null)
{
System.out.println (currNode.data + “ ”);
currNode = currNode.next;
}
}
public static void main (String[] args)
LinkedList list = new LinkedList();
list = insert (list, 11);
list = insert (list, 12);
list = insert (list, 13);
list = insert (list, 14);
list = insert (list, 15);
printList (list);
}
}

Output: We will compile the program and also run it.

LinkedList: 11 12 13 14 15

Example 2: Here we give an example to create a linked list in Java.

public class SinglyLinkedList {    
    class Node{    
 int data;    
Node next;    
 public Node(int data) {    
this.data = data;    
  this.next = null;    
        }    
    }   
    public Node head = null;    
    public Node tail = null;
    public void addNode(int data) {   
        Node newNode = new Node(data);    
                    if(head == null) {    
            head = newNode;    
            tail = newNode;    
        }    
        else {    
            tail.next = newNode;    
            tail = newNode;    
        }    
    }    
            public void display() {
        Node current = head;    
             if(head == null) {    
            System.out.println("List is empty");    
            return;    
        }    
        System.out.println("The Singly linked list are: ");    
        while(current != null) {    
            System.out.print(current.data + " ");    
            current = current.next;    
        }    
        System.out.println();    
    }    
        public static void main(String[] args) {  
        SinglyLinkedList sList = new SinglyLinkedList();    
        sList.addNode(111);    
        sList.addNode(112);    
sList.addNode(113);    
        sList.addNode(114);      
        sList.addNode(115);      
        sList.display();    
    }    
}    

Output: We will compile the program and also run it.

The Singly linked list are: 111 112 113 114 115

Related Topics

What is Core Java?

The fundamental Java, which includes the fundamental idea of the Java programming language, is referred to as "Core Java." The definition of "Core" is the core idea of something. Core...

3 minutes read.

Sylvester Sequence in Java

A Sylvester Sequence is a series of numbers in which each term is the sum of the terms before it plus 1. The sequence's first two terms are 2 and...

3 minutes read.

Java Thread Creation

Java provides the two ways to create a Thread: Implementing the Runnable interface.Extending the Thread class. Implementing Runnable interface The easiest way of creating a thread is to make a class that implements...

5 minutes read.

MVC in Java

A well-known design pattern is Model-View-Controller. The discipline of web development. We can organize our code in this manner. The document stipulates that a program or application must include a...

4 minutes read.

Java Math pow() Method

The pow() method of Math class returns the value of first argument raised to the power of second argument i.e. ab. Syntax: public static double pow(double a, double b) Parameters: The parameters ‘a’ and...

3 minutes read.

Anagram Program in Java using String

Anagram Program in Java Anagrams are those words that are generated by rearrangement of the letters of another phrase or word. Usually, while doing the rearrangement, the original letters are used...

8 minutes read.

Flag Pattern in Java

The flag pattern in Java can be printed, which will be covered in this part. Given how difficult they are to code, flag patterns are rarely asked by interviewers. We separate...

2 minutes read.

Arithmetic Operations on String in Java

Introduction Arithmetic, Relational, Bitwise, and Logical operators are all available in Java. Simple mathematical calculations are performed using Java arithmetic operators. Basic Arithmetic operators are considered in Java to be Addition,...

4 minutes read.

Java Math.multiplyExact() method in Java

Java has an inbuilt math function called Math.multiplyExact() that returns the sum of the parameters. If the result exceeds an integer, an exception is thrown. There is no need to...

2 minutes read.

Process and Thread in Java

Process It is a program that is running on your computer. The process is a heavy-weight, which takes memory separately to other processes. The process may be a small background task like spell-checker; it...

11 minutes read.

Misc Operators in Java

In this article, we are going to learn about the misc operators in Java. Misc operators are nothing but the miscellaneous operators. The java programming language supports some of the...

4 minutes read.

Java vs Scala

Java : Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say...

4 minutes read.

Counting sort in Java

An array's elements are sorted using the counting sort method, which counts how many times each distinct element appears in the array. The count is kept in an auxiliary array,...

3 minutes read.

Group by in Java 8

By using this groupingBy() method, developers can directly able to perform the "GROUP BY" operation. The thing is, now, the Java 8 programming language allows the programmers to do this...

3 minutes read.

What is new in Java 17?

Java 17 LTS is the most recent long-term support release for the Java SE platform. Under the Oracle No-Fee Terms and Conditions License, which stands for long-term support, JDK 17...

6 minutes read.

Java Program to Sort an Array of 0's, 1's, and 2’s | Dutch National Flag Problem in Java

The famed Dutch computer programmer Edsger Dijkstra's Dutch Nation Flag (DNF) challenge ranks among the most well-known programming challenges. The Dutch tricolor flag, comprising red, white, and blue, is the...

3 minutes read.

Math fma () method in java

In java, the Math module constitutes of fma () Method in it. This method can be accessed in two ways which can be differentiated by the parameters which are given...

4 minutes read.

Bellman Ford Algorithm in Java

Numerous algorithms have been used in dynamic programming to determine the shortest path inside a graph. Among them are Floyd, all-pair shortest path problem, Breadth First Search, Depth First Search,...

6 minutes read.

Java md5 Hash Example

A 128-bit hash value is generated by the Message Digest Algorithm 5, which is a cryptographic algorithm. A stationary hash value is generated by the hash function from data of...

3 minutes read.

Java Set to List

In this article, you will be acknowledged about how the process of conversion from Set or HashSet to LinkedList happens and what are the possible ways involved in conversion process. First...

4 minutes read.