Problem statement: Delete duplicate-value nodes from a sorted linked list
Input Format:
You have to complete the Node* RemoveDuplicates(Node* head) method which takes one argument - the head of the sorted linked list. You should NOT read any input from stdin/console.
Output Format:
Delete as few nodes as possible to ensure that no two nodes have the same data. Adjust the next pointers to ensure that the remaining nodes form a single sorted linked list. Then return the head of the sorted updated linked list.
My Code:
Node RemoveDuplicates(Node head) {
Node n = head;
while(n.next!=null){
Node test = n.next;
while(n.data==test.data){
if(test.next!=null){
n.next = test.next;
test = n.next;
}
else{
n.next = null;
}
}
if((n.next!=null)){
n = n.next;
}
}
return head;
}
When tested, it runs perfectly except when the last node's value is equal to previous node's value. I couldn't find the mistake in my code.
Test results:

The first int is the number of test cases and second int is the number of nodes in the list.
Problem taken from HackerRank.