Java solution recursive, 1ms


  • 0

    This runtime is 1ms:

    public class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            if (head == null ) return null;
            if(head.next == null) return head;
            
            while (head.next != null && head.val == head.next.val) {
               head = head.next;
            } 
             head.next = deleteDuplicates(head.next);
            
            return head;
        }
    }
    

    But, this one is 2ms?

    public class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            if (head == null || head.next == null) return head;
            
            while (head.next != null && head.val == head.next.val) {
               head = head.next;
            } 
             head.next = deleteDuplicates(head.next);
            
            return head;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.