My code is working for 1 and 2 digit numbers but not for numbers > 2 digits
public class remove_duplicates {
public static node<Integer> takeInput() {
Scanner s = new Scanner(System.in);
int data = s.nextInt();
node<Integer> head = null;
while (data != -1){
node<Integer> newNode =new node<Integer>(data);
if(head == null){
head = newNode;
}
else {
node<Integer> temp = head;
while(temp.next != null){
temp = temp.next;
}
temp.next =newNode;
}
data = s.nextInt();
}
return head;
}
public static node<Integer> removeDuplicates(node<Integer> head){
node<Integer> current = head;
while(current.next != null){
if(current.data == current.next.data){
current.next = current.next.next;
}
else
current = current.next;
}
return head;
}
public static void print (node<Integer>head){
node<Integer> temp = head;
while (temp != null)
{
System.out.print(temp.data+" ");
temp = temp.next;
}
System.out.println();
}
public static void main(String[] args) {
node<Integer> head = takeInput();
node<Integer> data = removeDuplicates(head);
print(data);
}
}
My output: 281 386 386 957 1022 1216 1232 1364 1428 1428 1428 1428 1501 1953
Expected output: 281 386 957 1022 1216 1232 1364 1428 1501 1953
Why is it working for 1/2 digits integers and not for 3 digits or more? How can I solve this issue?