Java Program to Add a Node to a Singly Linked List

In this article we are going to see how we can add a node to a singly linked list by using Java programming language.

Java Program to Add a Node to a Singly Linked List

In a linked list each element is called as a node which is having the actual element and a pointer to next element. You can add a new node at any position of the linked list.

Approach:

  • Create a linked list add some elements.
  • Display the list.
  • Add elements at the beginning by using the addBeg() user defined method that points the head to the new node and links the node to the rest of the list.
  • Display the list.
  • Ask the user to enter the position and data to insert.
  • Pass the values with head to the addPos( ) user defined method. The method iterates to the position and adds the node to the rest of the list.
  • Display the list.
  • Insert elements at the end by using insertEnd( ) user defined method. The method updates the tail and links the new node.
  • Display the list.

Program:

import java.util.*;
// Main class
public class Main
{
    // Class Node that defines the two linked list variables
    class Node
    {
        int data;
        Node nextNode;
        // constructor to create a node
        public Node(int data) 
        {    
            this.data = data;    
            this.nextNode = null;    
        }    
    }

    // Setting the head and end of the node as NULL  
    public Node head = null;    
    public Node tail = null;  

    // Method to add a node to the start of a linked list
    public void addBeg(int data)
    {  
        //Create a new node  
        Node newNode = new Node(data);  
        //Checks if the list is empty  
        if(head == null)
        {  
            // makes the head and tail point to the first node
            head = newNode;  
            tail = newNode;  
        }  
        else
        {
            Node temp = head;  
            //The newNode becomes the head of the linked list
            head = newNode;  
            //the head node now points to the previous head node
            head.nextNode = temp;  
        }  
    }

    // Method to add a node at a specified position
    public void addPos(Node headNode, int pos, int data) 
    {
        Node curr = headNode;
        // Position should be always greater than equals to 1
        if (pos < 1)
            System.out.print("Wrong input");
 
        // if position is 1 then the insertion happens at head
        if (pos == 1) 
        {
            // Setting the newNode as head
            Node newNode = new Node(data);
            newNode.nextNode = headNode;
            curr = newNode;
        } 
        else 
        {
            while (pos-- != 0) 
            {
                if (pos == 1) 
                {
                    Node newNode = new Node(data);
                    // Set the new node to point to the previously existing node
                    newNode.nextNode = headNode.nextNode;
                    headNode.nextNode = newNode;
                    break;
                }
                headNode = headNode.nextNode;
            }
            if (pos != 1)
                System.out.print("Wrong input");
        }
        head = curr;
    }

    // method to add a node to the end of the linked list
    public void add(int data)
    {
        Node newNode = new Node(data);
        // Checks if there was any previous node
        if(head==null)
        {
            head = newNode;
            tail = newNode;
        }
        else
        {
            tail.nextNode = newNode;
            tail = newNode;
        }
    }
    
    // Method to display all the nodes of the linked list
    public void show()
    {
        Node curr = head;
        // If the head is pointing to no node then the linked list is empty
        if(head==null)
            System.out.println("Linked List is empty");
        else
        {
            System.out.println("The nodes are:");
            while(curr!=null)
            {
                System.out.print(curr.data+",");
                curr = curr.nextNode;
            }
            System.out.println();
        }
    }

    public static void main(String[] args) 
    {
        // create an object of the main class
        Main ll = new Main();
        // add elements to the linked list
        ll.add(10);
        ll.add(20);
        ll.add(30);
        ll.add(40);
        ll.add(50);
        // display the nodes
        ll.show();
        // add a node at beginning
        System.out.println("Adding 11 at beginning");
        ll.addBeg(11);
        // Display the nodes
        ll.show();
        // Add a node at a position
        // Taking user input
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the position to enter a node and the data");
        int pos = sc.nextInt(), data = sc.nextInt();
        ll.addPos(ll.head,pos,data);
        // display the nodes
        ll.show();
        // Add a node at the end
        System.out.println("Adding 99 at end");
        ll.add(99);
        // display the nodes
        ll.show();
    }
}
Output:

The nodes are:
10,20,30,40,50,
Adding 11 at beginning
The nodes are:
11,10,20,30,40,50,
Enter the position to enter a node and the data
5 50
The nodes are:
11,10,20,30,50,40,50,
Adding 99 at end
The nodes are:
11,10,20,30,50,40,50,99,

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.