Insertion in Beginning in a Linked List in Java

Insertion in Beginning in JAVA

Collection of Data Elements along with the address to their consecutive element is called a Linked List. For Insertion in Beginning in A Linked List in JAVA We’ll first have to store the address to the previous first element along with the new element that will be inserted into the list. And also the head will now point towards the address of the new element.

Insertion In Beginning of a Linked List

Implementation

To insert a node in the beggining of a linked list, we first have to check the Head’s Reference to the first node of a linked list.

 If the head is equal to null, then the list is already empty else the list already has an element whose reference is stored by the head. 

To insert an element, in the beginning, we will have to replace the address stored by the head with the address of the new element we wish to insert. 

The address space of the previously stored element will now be stored in the pointer reference of the inserted element.

Linked List Insertion at Beginning in Java

Algorithm for Insertion in Beginning in A Linked List in JAVA

  • IF HEAD == NULL
    EXIT
  • ELSE
  • NEW_NODE = ADDNODE()
  • NEW_NODE -> DATA = ITEM
  • PTR = HEAD
  • NEW -> NEXT = PTR
  • HEAD = NEW_NODE
  • EXIT
Singly Linked List Insertion at the beginning in Java
This method uses objects to call functions
Run
import java.lang.*;

class LinkedList {
    Node head;

    // Node Class
    class Node{
        int data;
        Node next;

        Node(int x) // parameterized constructor
        {
            data = x;
            next = null;
        }
    }
    public Node insertStart(int data)
    {
        // Creating newNode memory & assigning data value
        Node newNode = new Node(data);
        // assigning this newNode's next as current head node
        newNode.next = head;

        // re-assigning head to this newNode
        head = newNode;

        System.out.println(newNode.data + " inserted");
        return head;
    }


    public void display()
    {
        System.out.println();
        Node node = head;
        //as linked list will end when Node reaches Null
        while(node!=null)
        {
            System.out.print(node.data + " ");
            node = node.next;
        }
        System.out.println();
    }
}
public class Main
{
    public static void main(String args[])
    {
        LinkedList ll = new LinkedList();

        ll.insertStart(5);
        ll.insertStart(4);
        ll.insertStart(3);
        ll.insertStart(2);
        ll.insertStart(1);

        ll.display();

    }
}

Output

5 inserted
4 inserted
3 inserted
2 inserted
1 inserted

1 2 3 4 5
This method static functions that are called from static main.
Run
import java.lang.*;

// Node Class
class Node {
    int data;
    Node next;

    Node(int x)
    {
        data = x;
        next = null;
    }
}

class Main
{
    static Node insertStart(Node head, int data)
    {
        // Creating newNode memory & assigning data value
        Node newNode = new Node(data);

        // assigning this newNode's next as current head node
        newNode.next = head;
        // re-assigning head to this newNode
        head = newNode;

        System.out.println(newNode.data + " inserted");
        return head;
    }

    static void display(Node node) {
        System.out.println();
        //as linked list will end when Node is Null
        while (node != null) {
            System.out.print(node.data + " ");
            node = node.next;
        }
        System.out.println();
    }

    public static void main(String args[])
    {
        Node head = null;
        head = insertStart(head,5);
        head = insertStart(head,4);
        head = insertStart(head,3);
        head = insertStart(head,2);
        head = insertStart(head,1);

        display(head);

    }
}

Output

5 inserted
4 inserted
3 inserted
2 inserted
1 inserted

1 2 3 4 5