mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-12 14:42:03 +08:00
Add remove duplicate nodes from a sorted linked list algorithm (#2452)
This commit is contained in:
53
DataStructures/Lists/RemoveDuplicateNodes.java
Normal file
53
DataStructures/Lists/RemoveDuplicateNodes.java
Normal file
@ -0,0 +1,53 @@
|
||||
package DataStructures.Lists;
|
||||
|
||||
import DataStructures.Lists.Node;
|
||||
|
||||
public class RemoveDuplicateNodes {
|
||||
|
||||
public Node deleteDuplicates(Node head) {
|
||||
// sentinel
|
||||
Node sentinel = new Node(0, head);
|
||||
|
||||
// predecessor = the last node
|
||||
// before the sublist of duplicates
|
||||
Node pred = sentinel;
|
||||
|
||||
while (head != null) {
|
||||
// if it's a beginning of duplicates sublist
|
||||
// skip all duplicates
|
||||
if (head.next != null && head.value == head.next.value) {
|
||||
// move till the end of duplicates sublist
|
||||
while (head.next != null && head.value == head.next.value) {
|
||||
head = head.next;
|
||||
}
|
||||
// skip all duplicates
|
||||
pred.next = head.next;
|
||||
// otherwise, move predecessor
|
||||
} else {
|
||||
pred = pred.next;
|
||||
}
|
||||
|
||||
// move forward
|
||||
head = head.next;
|
||||
}
|
||||
return sentinel.next;
|
||||
}
|
||||
|
||||
public void print(Node head) {
|
||||
Node temp = head;
|
||||
while (temp != null && temp.next != null) {
|
||||
System.out.print(temp.value + "->");
|
||||
temp = temp.next;
|
||||
}
|
||||
if (temp != null) {
|
||||
System.out.print(temp.value);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String arg[]) {
|
||||
RemoveDuplicateNodes instance = new RemoveDuplicateNodes();
|
||||
Node head = new Node(0, new Node(2, new Node(3, new Node(3, new Node(4)))));
|
||||
head = instance.deleteDuplicates(head);
|
||||
instance.print(head);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user