Remove duplicates from sorted list

Report a typo

Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

The code uses the same node structure described in the theory.
class ListNode{
    int val;
    public ListNode next;
    public ListNode(int val){
        this.val = val;
    }
    public boolean hasNext(){
        return next != null;
    }
}

Sample Input 1:

1 1 1 2 3 3 10 14 14 24

Sample Output 1:

1 2 3 10 14 24
Write a program in Java 25
static ListNode removeDuplicates(ListNode head){
//Write your code here
}

Create a free account to access the full topic