package com.thealgorithms.datastructures.lists;
public class RemoveDuplicateNodes {
public Node deleteDuplicates(Node head) {
Node sentinel = new Node(0, head);
Node pred = sentinel;
while (head != null) {
if (head.next != null && head.value == head.next.value) {
while (head.next != null && head.value == head.next.value) {
head = head.next;
}
pred.next = head.next;
} else {
pred = pred.next;
}
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);
}
}