w3resource

Java Exercises: Delete a specified node in the middle of a singly linked list

Java Basic: Exercise-175 with Solution

Write a Java program to delete a specified node in the middle of a singly linked list.

Sample Singly linked list: 10->20->30->40->50
Delete the fourth node i.e. 40
Result: 10->20->30->50

Pictorial Presentation:

Java Basic Exercises: Delete a specified node in the middle of a singly linked list.

Sample Solution:

Java Code:

import java.util.*;
import java.util.Arrays;
import java.util.LinkedList;

public class Solution {
 public static ListNode head = new ListNode(10);

 public static void main(String[] args) {
  head.next = new ListNode(20);
  head.next.next = new ListNode(30);
  head.next.next.next = new ListNode(40);
  head.next.next.next.next = new ListNode(50);
  ListNode p = head;
  System.out.println("Original Linked list:");
  printList(p);
  System.out.println("\nAfter deleting the fourth node, Linked list becomes:");
  deleteNode(head.next.next.next);
  p = head;
  printList(p);
 }

 public static void deleteNode(ListNode node) {
  if (node.next != null) {
   int temp = node.val;
   node.val = node.next.val;
   node.next.val = temp;

   node.next = node.next.next;
  } else {
   ListNode p = head;
   while (p.next.val != node.val) {
    p = p.next;
   }
   p.next = null;
  }
 }

 static void printList(ListNode p) {

  while (p != null) {
   System.out.print(p.val);
   if (p.next != null) {
    System.out.print("->");
   }
   p = p.next;
  }
 }
}
class ListNode {
 int val;
 ListNode next;

 ListNode(int val) {
  this.val = val;
  this.next = null;
 }
}

Sample Output:

Original Linked list:
10->20->30->40->50
After deleting the fourth node, Linked list becomes:
10->20->30->50

Flowchart:

Flowchart: Java exercises: Delete a specified node in the middle of a singly linked list.

Java Code Editor:

Company:  Adobe Apple Microsoft

Contribute your code and comments through Disqus.

Previous: Write a Java program to find the maximum number inside the number in the window (size k) at each moving in a given array of intergers with duplicate numbers. Move the window from the start of the array.
Next: Write a Java program to partition an given array of integers into even number first and odd number second.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

Java: Tips of the Day

How to remove leading zeros from alphanumeric text?

Regex is the best tool for the job; what it should be depends on the problem specification. The following removes leading zeroes, but leaves one if necessary (i.e. it wouldn't just turn "0" to a blank string).

s.replaceFirst("^0+(?!$)", "")

The ^ anchor will make sure that the 0+ being matched is at the beginning of the input. The (?!$) negative lookahead ensures that not the entire string will be matched.

Test harness:

String[] in = {
    "01234",         // "[1234]"
    "0001234a",      // "[1234a]"
    "101234",        // "[101234]"
    "000002829839",  // "[2829839]"
    "0",             // "[0]"
    "0000000",       // "[0]"
    "0000009",       // "[9]"
    "000000z",       // "[z]"
    "000000.z",      // "[.z]"
};
for (String s : in) {
    System.out.println("[" + s.replaceFirst("^0+(?!$)", "") + "]");
}

Ref: https://bit.ly/2Qdcl8a