Delete nth node in Linked List in Java
Java Program for Deletion from the nth position in a Linked List
Lets have a look on different methods that we can use for Deletion from the nth position in a Linked List in JAVA.
Implementation
- Take the Linked List input from the user and calculate the length of the Linked List
- Accept the nth node that the user wants to delete
- Check if the value of n is valid should not be negative or 0 and should not exceed the size of Linked List
- If the user wants to delete the first node then just change the header to next in the Linked List
- Else go ahead and traverse the Linked List to the nth node
- Change the next of (n-1)th node to (n+1)th node
- Done we have deleted the nth node its memory will automatically be deleted since there are no references to it.
Code In JAVA Programming Language
Method 1
Method 2
Method 1
The following method uses class object to call the non static member functions of the class
Run
import java.lang.*; class LinkedList { Node head; // Node Class class Node{ int data; Node next; Node(int val) { data = val; next = null; } } public void deleteNthNode(int n) { int len = calcLen(head); // Can only insert after 1st position // Can't insert if position to insert is greater than size of Linked List if(n < 1 || n > len) { System.out.println("Can't delete\n"); } else { if(n == 1) { // head has to be deleted System.out.println("Deleted: " + head.data); head = head.next; return; } // required to traverse Node temp = head; Node previous = null; // traverse to the nth node while(--n > 0) { previous = temp; temp = temp.next; } // assigned next node of the previous node to nth node's next previous.next = temp.next; System.out.println("Deleted: " + temp.data); } } public void insert(int data) { Node newNode = new Node(data); newNode.next = head; head = newNode; } public void showList() { Node temp = head; //as linked list will end when Node reaches Null while(temp!=null) { System.out.print(temp.data + " "); temp = temp.next; } System.out.println("\n"); } public int calcLen(Node temp){ int len = 0; while(temp!=null){ temp = temp.next; len++; } return len; } } public class Main { public static void main(String args[]) { LinkedList ll = new LinkedList(); ll.insert(35); ll.insert(34); ll.insert(33); ll.insert(32); ll.insert(31); ll.insert(30); ll.showList(); ll.deleteNthNode(3); ll.deleteNthNode(4); ll.showList(); } }
Output
30 31 32 33 34 35 Deleted: 32 Deleted: 34 30 31 33 35
Method 2
The following method uses static functions that are called with header references passed in them
Run
import java.lang.*; // Node Class class Node { int data; Node next; Node(int x) // parameterized constructor { data = x; next = null; } } class Main { static Node deleteNthNode(int n, Node head) { int len = calcLen(head); // Can only insert after 1st position // Can't insert if position to insert is greater than size of Linked List if(n < 1 || n > len) System.out.println("Can't delete\n"); else { if(n == 1) { // head has to be deleted System.out.println("Deleted: " + head.data); head = head.next; return head; } // required to traverse Node temp = head; Node previous = null; // traverse to the nth node while(--n > 0) { previous = temp; temp = temp.next; } // assigned next node of the previous node to nth node's next previous.next = temp.next; System.out.println("Deleted: " + temp.data); } return head; } public static Node insert(Node head, int data) { Node newNode = new Node(data); newNode.next = head; head = newNode; return head; } static void showList(Node temp) { //as linked list will end when Node is Null while (temp != null) { System.out.print(temp.data + " "); temp = temp.next; } System.out.println("\n"); } static int calcLen(Node node){ int len = 0; while(node!=null) { node = node.next; len++; } return len; } public static void main(String args[]) { Node head = null; head = insert(head, 35); head = insert(head, 34); head = insert(head, 33); head = insert(head, 32); head = insert(head, 31); head = insert(head, 30); showList(head); head = deleteNthNode( 3, head); head = deleteNthNode( 4, head); showList(head); } }
Output
30 31 32 33 34 35 Deleted: 32 Deleted: 34 30 31 33 35
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
Singly Linked List
- Introduction to Linked List in Data Structure
Click Here - Linked List in –
- Singly Linked List in –
- Insertion in singly Linked List –
- Insertion at beginning in singly Linked List –
- Insertion at nth position in singly Linked List –
- Insertion at end in singly Linked List –
- Deletion in singly Linked List –
- Deletion from beginning in singly linked list :
- Deletion from nth position in singly linked list :
- Deletion from end in singly linked list :
- Linked List Insertion and Deletion –
C | C++ | Java - Reverse a linked list without changing links between nodes (Data reverse only) –
C | C++ | Java - Reverse a linked list by changing links between nodes –
- Print reverse of a linked list without actually reversing –
- Print reverse of a linked list without actually reversing –
- Insertion in the middle Singly Linked List –
- Insertion in a Sorted Linked List –
- Delete alternate nodes of a Linked List –
- Find middle of the linked list –
- Reverse a linked list in groups of given size –
- Find kth node from end of the linked list –
- Append the last n nodes of a linked list to the beginning of the list –
- Check whether linked list is palindrome or not –
- Fold a Linked List –
- Insert at given Position –
- Deletion at given Position –
Singly Linked List
- Introduction to Linked List in Data Structure
- Linked List in – C | C++ | Java
- Singly Linked List in – C | C++ | Java
- Insertion in singly Linked List – C | C++ | Java
- Deletion in singly Linked List – C | C++ | Java
- Reverse a linked list without changing links between nodes (Data reverse only) – C | C++ | Java
- Linked List Insertion and Deletion – C | C++ | Java
- Reverse a linked list by changing links between nodes – C | C++ | Java
- Linked List insertion in the middle – C | C++ | Java
- Print reverse of a linked list without actually reversing – C |C++ | Java
- Search an element in a linked list – C | C++ | Java
- Insertion in a Sorted Linked List – C | C++ | Java
- Delete alternate nodes of a Linked List – C | C++ | Java
- Find middle of the linked list – C | C++ | Java
- Reverse a linked list in groups of given size – C | C++ | Java
- Find kth node from end of the linked list – C | C++ | Java
- Append the last n nodes of a linked list to the beginning of the list – C | C++ | Java
- Check whether linked list is palindrome or not – C | C++ | Java
- Fold a Linked List – C | C++ | Java
- Insert at a given position – C | C++ | Java
- Delete at a given position – C | C++ | Java
Login/Signup to comment