Insert node at end of linked list in java

Write a program which is create and add linked list node at the end (tail, last) position in java language.

Suppose we are inserted the following (10, 20, 30 ,40 ,50) node in a sequence.

insert node at end of linked list
// Java Program for
// Insert linked list element at end position set A

// 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()
    {
        // Set inital value
        this.head = null;
    }
    // Add new node at the end of linked list
    public void addNode(int value)
    {
        // Create  node
        LinkNode node = new LinkNode(value);
        if (this.head == null)
        {
            this.head = node;
        }
        else
        {
            LinkNode temp = this.head;
            // Find last node
            while (temp.next != null)
            {
                // Visit to next node
                temp = temp.next;
            }
            // Add node at last position
            temp.next = node;
        }
    }
    // Display all Linked List elements
    public void display()
    {
        if (this.head != null)
        {
            LinkNode temp = this.head;
            while (temp != null)
            {
                // Display node value
                System.out.print("  " + temp.data);
                // Visit to next node
                temp = temp.next;
            }
        }
        else
        {
            System.out.print("Empty Linked list\n");
        }
    }
    public static void main(String[] args)
    {
        SingleLL sll = new SingleLL();
        // Linked list
        // 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
        sll.addNode(1);
        sll.addNode(2);
        sll.addNode(3);
        sll.addNode(4);
        sll.addNode(5);
        sll.addNode(6);
        sll.addNode(7);
        sll.addNode(8);
        System.out.print(" Linked List \n");
        sll.display();
    }
}
Linked List
1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL

Time complexity of above program is O(n). We can optimize above algorithm using one extra pointer. Which is hold the reference of last node. Below are implementation of this logic.

// Java Program for
// Insert linked list element at end position set B

// 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 LinkNode tail;
    public SingleLL()
    {
        // Set head and tail 
        this.head = null;
        this.tail = null;
    }
    // Add new node at the end of linked list
    public void addNode(int value)
    {
        // Create a new node
        LinkNode node = new LinkNode(value);
        if (this.head == null)
        {
            this.head = node;
        }
        else
        {
            this.tail.next = node;
        }
        this.tail = 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
        // 10 → 20 → 30 → 40 → 50 → null
        sll.addNode(10);
        sll.addNode(20);
        sll.addNode(30);
        sll.addNode(40);
        sll.addNode(50);
        System.out.print("Linked List \n");
        sll.display();
    }
}
 Linked List
 10 → 20 → 30 → 40 → 50 → NULL
Java insert linked list node at end

Time complexity of above program is O(1).



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







© 2021, kalkicode.com, All rights reserved