Java program to remove duplicate elements from a singly linked list17 Mar 2025 | 4 min read In this program, we need to remove the duplicate nodes from the given singly linked list. Original List: ![]() List after removing duplicate nodes: ![]() In the above list, node 2 is repeated thrice, and node 1 is repeated twice. Node current will point to head, and index will point to node next to current. Start traversing the list till a duplicate is found that is when current's data is equal to index's data. In the above example, the first duplicate will be found at position 4. Assign current to another node temp. Connect temp's next node with index's next node. Delete index which was pointing to duplicate node. This process will continue until all duplicates are removed. Algorithm
a. removeDuplicate() will remove duplicate nodes from the list.
a. display() will display the nodes present in the list:
Program:Output: Originals list: 1 2 3 2 2 4 1 List after removing duplicates: 1 2 3 4 Next TopicJava Programs |
Java Convert Date to String We can convert Date to String in java using format() method of java.text.DateFormat class. format() method of DateFormat The format() method of DateFormat class is used to convert Date into String. DateFormat is an abstract class. The child class of DateFormat is SimpleDateFormat. It...
2 min read
Duck number is another special positive non-zero number that contains zero in it. The digit zero should not be presented at the starting of the number. Zero can be present at any of the positions except the beginning of the number. Let's understand some examples of Duck...
3 min read
In this program, we will create a doubly linked list and delete a node from the end of the list. If the list is empty, print the message "List is empty". If the list is not empty, tail's ious node will become the new tail...
6 min read
? It is a very interesting problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, IBM etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are...
2 min read
The Evil number is another special positive whole number in Java that has an even number of 1's in its binary equivalent. Unlike Prime and Armstrong numbers, Evil number is not so popular and asked by the interviewers. The numbers which are not evil are called odious...
3 min read
In this section, we will write the Java programs to check if a number is positive or negative. We have used the following ways to check the number is positive, negative, or zero. Using Relational Operator Using Math.signum() Method Using Integer.signum() Method Using Bit Shift Operator Using ArrayList class Using Relational Operator To...
5 min read
In this program, we need to sort the given array in ascending order such that elements will be arranged from smallest to largest. This can be achieved through two loops. The outer loop will select an element, and inner loop allows us to compare selected...
4 min read
The object is a basic building block of an OOPs language. In Java, we cannot execute any program without creating an object. There is various way to create an object in Java that we will discuss in this section, and also learn how to create...
7 min read
It is a very interesting problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, Adobe, Apple, Infosys, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section,...
5 min read
How do you find duplicate characters in a string? Following program demonstrate it. File: DuplicateCharFinder .java import java.util.HashMap; import java.util.Map; import java.util.Set; public class DuplicateCharFinder { public void findIt(String str) { Map<Character, Integer> baseMap = new HashMap<Character, Integer>(); char[] charArray = str.toCharArray(); for (Character ch : charArray) { if (baseMap.containsKey(ch)) { baseMap.put(ch, baseMap.get(ch) + 1); } else...
1 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India