Skip to main content

Insert node at beginning of linked list java

Write a program which is create and add new node at the beginning of linked list in java.

insert node at beginning of linked list

Here given of code implementation process.

/*
    Java program for
    Insert node at beginning of linked list
*/
// Linked list node
class LinkNode
{
    public int data;
    public LinkNode next;
    public LinkNode(int data)
    {
        this.data = data;
        this.next = null;
    }
}
public class SingleLL
{
    public LinkNode head;
    public SingleLL()
    {
        this.head = null;
    }
    // Adding new node at beginning of linked list
    public void addNode(int data)
    {
        // Create new node
        LinkNode node = new LinkNode(data);
        // Connect current node to previous head
        node.next = this.head;
        this.head = node;
    }
    // Display linked list element
    public void display()
    {
        if (this.head == null)
        {
            return;
        }
        LinkNode temp = this.head;
        // iterating linked list elements
        while (temp != null)
        {
            System.out.print(" " + temp.data + " →");
            // Visit to next node
            temp = temp.next;
        }
        System.out.print(" NULL\n");
    }
    public static void main(String[] args)
    {
        SingleLL sll = new SingleLL();
        // Linked list
        // 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
        sll.addNode(8);
        sll.addNode(7);
        sll.addNode(6);
        sll.addNode(5);
        sll.addNode(4);
        sll.addNode(3);
        sll.addNode(2);
        sll.addNode(1);
        sll.display();
    }
}
Visualize the insert node beginning of singly linked list
1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL

Time complexity of above program is O(1).

This is a Java program for inserting a node at the beginning of a singly linked list. The program defines a LinkNode class to represent a node in the linked list, with fields for the node's data and a reference to the next node in the list. It also defines a SingleLL class to represent the linked list itself, with a head field to store a reference to the first node in the list.

The program's main method creates a new SingleLL object and adds nodes to it using the addNode method. Each time addNode is called, it creates a new LinkNode object with the specified data, sets its next field to the current head of the linked list, and then sets the head of the list to the new node. This effectively inserts the new node at the beginning of the list.

The display method of the SingleLL class is used to print the contents of the linked list to the console. It iterates over each node in the list and prints its data, along with an arrow indicating the direction of the next node in the list.

When the main method is executed, it creates a linked list with nodes containing the values 1 through 8, in descending order. It then calls the display method to print the list to the console, which should output the following:

1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment