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.
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.
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
Method 1
Method 2
Method 1
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 insertBeginning(int data) { Node newNode = new Node(data); newNode.next = head; head = newNode; System.out.println("inserted "+data); 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.insertBeginning(5); ll.insertBeginning(4); ll.insertBeginning(3); ll.insertBeginning(2); ll.insertBeginning(1); ll.display(); } }
Output
inserted 5 inserted 4 inserted 3 inserted 2 inserted 1 1 2 3 4 5
Method 2
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(" inserted "+newNode.data); 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
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment