Remove duplicates from linked list recursively

Viewed 30
public class Solution {

    public static LinkedListNode<Integer> removeDuplicates(LinkedListNode<Integer> head) {
        //Your code goes here
        if(head == null || head.next == null){
            return head;
        }
        head.next = removeDuplicates(head.next);
        if(head.data == head.next.data){
            LinkedListNode<Integer>node = head.next;
            head.next = head.next.next;
            node.next = null;
        }
        
        
        return head;
    }

}

This code is failing for some test cases. what am i doing wrong?

0 Answers
Related