×

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

Java Technologies List

Introducing Java technology is not necessary. Everyone across the globe is still in awe of Java's incredible capabilities for developing mobile apps and websites. Of course, you can be persuaded...

8 minutes read.

Iccanobif Numbers in Java

In this article, you will be very well equipped with the knowledge of iccanobif numbers in Java. You will also know how they are formed and basic example programs on...

3 minutes read.

Lambda expressions in Java

A brief introduction to Lambda expression in java In this topic, we will discuss the lambda expression in java. A lambda expression in Java is an enhanced version of an anonymous...

13 minutes read.

this keyword in Java

The 'this' keyword is an essential notion in Java. We'll go through the 'this' keyword in depth and provide some examples of how it's used in Java. In Java, the term...

5 minutes read.

How to reverse a linked list in java

The process of reversing a linked list in Java will be covered in this section. One of the most common questions in interviews is about reversing a linked list. If...

11 minutes read.

Java Command not found

Java Command not found error is displayed if Java is not installed on the computer or if the command prompt cannot find Java.exe to run the program. Our Java software...

3 minutes read.

Java Math acos() Method

The acos() method of Math class computes the trigonometric Arc Cosine (inverse of cosine ) of an angle. The value returned is between 0.0 to pi. Syntax: public static double acos(double a) Parameters: The...

1 minute read.

Inheritance Program in Java

Inheritance Program in Java Inheritance is one of the important pillars of Object-Oriented Programming that facilitates parent-child relationships in programming. Using inheritance, we can create a new class with the help...

7 minutes read.

Sudoku in Java

Sudoku is a combinatorial-number-placement puzzle with a logic-based approach. The goal of a traditional Sudoku puzzle is to fill in the numbers on a 9 by 9 grid so that...

6 minutes read.

Java Naming Conventions

JAVA NAMING CONVENTIONS Java naming convention is a standard pattern for writing your identifier name such as class, interfaces, methods, constants, variable, etc. These patterns are not standard rules that you must...

2 minutes read.

Merge Sort in Java

Merge Sort in Java Merge sort in Java uses the divide and conquer approach to sort the given array/ list. There are three steps involved in the merge sort. 1) Divide the...

5 minutes read.

Different Ways to Take Input from User in Java

Any information provided to a system for use is known as user input. Any responsive software or application must include user input. In Java, there are 4 different ways to...

5 minutes read.

String Palindrome Program in Java

String Palindrome Program in Java The palindrome is a string, phrase, word, number, or other sequences of characters that can be read in both directions i.e. forward (left to right) and...

11 minutes read.

Map of Map in Java

The map is now a Java interface for mapping keys to values. It is frequently necessary to use Map of Map (nested Map). Nested Maps are useful in various situations, including...

3 minutes read.

Automorphic Number Program in Java

We will learn about automorphic numbers through examples in this article, and we'll also make Java programmes that can determine whether a specific number was automorphic or not. What is an...

3 minutes read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 minutes read.

What are Array strings in Java?

In normal programming, An array is a group and a collection of identical forms of data that are stored in a sequential memory region and may be accessed using their...

4 minutes read.

Monsoon Umbrella Problem in Java

The Monsoon Umbrella problem is a classic Java programming problem used to test the skills of a programmer. The problem involves writing a program to determine the number of umbrellas...

3 minutes read.

Java Primitive Data Types

Primitive data types are the simplest data types in a programming language. They’re predefined in the language. The names of the primitive types are quite descriptive of the values that...

2 minutes read.

if-else Program in Java

if-else Program in Java The if-else program in Java controls which code snippet will execute. The if-else program is very basic and yet very important. Because it checks how well one...

19 minutes read.