Insert A Node At Middle In Linked List using Java
Technology and generation are growing up together, and the younger generation users are somewhat connected with tech and the internet all the time. Not to mention, today the whole world in this time of crisis is working over the internet. But to make these technologies, software, etc a software developer must have excellent problem-solving skills. In this world where the internet is the new fuel, one needs to be pretty sharp. And by sharp, for software developers, it means knowing how to automate real-world problems using computer programs. Data structures help a lot in this journey of logic building. So today we’re going to write a simple data structure program to Insert A Node At the Middle In Linked List using Java.
What is A Linked List?
A linked list is a basic list of nodes containing different elements for all algorithms in data structures. Also, it’s pretty easy to understand.
What’s The Approach?
- Before inserting a new node we’ll first check whether the previous node is null or not. If the previous node is null then we’ll not add the new node.
- If the previous node is not null then we’ll simply create a new node and insert data into it.
- Now we’ll store the address which is stored in the previous node into this new node.
- At last, in the previous node’s address section we will store the address of this new node.
Also Read: Print Smallest Of Three Without Using Comparison Operator in Java
Java Program To Insert A Node At Middle In Linked List
Input:
append(6)
push(7)
insertAfter(8)
Output:
Created Linked List is: 7 8 6
// Java program to demonstrate insertion methods // on linked list at middle class LinkedList { Node head; // head of list /* Linked list Node*/ class Node { int data; Node next; Node(int d) {data = d; next = null; } } /* Inserts a new Node at front of the list. */ public void push(int new_data) { /* 1 & 2: Allocate the Node & Put in the data*/ Node new_node = new Node(new_data); /* 3. Make next of new Node as head */ new_node.next = head; /* 4. Move the head to point to new Node */ head = new_node; } /* Inserts a new node after the given prev_node. */ public void insertAfter(Node prev_node, int new_data) { /* 1. Check if the given Node is null */ if (prev_node == null) { System.out.println("The given previous node cannot be null"); return; } /* 2 & 3: Allocate the Node & Put in the data*/ Node new_node = new Node(new_data); /* 4. Make next of new Node as next of prev_node */ new_node.next = prev_node.next; /* 5. make next of prev_node as new_node */ prev_node.next = new_node; } /* Appends a new node at the end. This method is defined inside LinkedList class shown above */ public void append(int new_data) { /* 1. Allocate the Node & 2. Put in the data 3. Set next as null */ Node new_node = new Node(new_data); /* 4. If the Linked List is empty, then make the new node as head */ if (head == null) { head = new Node(new_data); return; } /* 4. This new node is going to be the last node, so make next of it as null */ new_node.next = null; /* 5. Else traverse till the last node */ Node last = head; while (last.next != null) last = last.next; /* 6. Change the next of last node */ last.next = new_node; return; } /* This function prints contents of linked list starting from the given node */ public void printList() { Node tnode = head; while (tnode != null) { System.out.print(tnode.data+" "); tnode = tnode.next; } } /* Driver program*/ public static void main(String[] args) { /* Start with the empty list */ LinkedList llist = new LinkedList(); // Insert 6. So linked list becomes 6->NUllist llist.append(6); // Insert 7 at the beginning. So linked list becomes // 7->6->NUllist llist.push(7); // Insert 8, after 7. So linked list becomes //7->8->6->NUllist llist.insertAfter(llist.head, 8); System.out.println("\nCreated Linked list is: "); llist.printList(); } }