I got exceed time limit. how can i improve the solution?


  • 0
    X
    public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) return head;
        ListNode nextNode = head.next;
        if (nextNode == null) return head;
        head.next = checkDup(nextNode); 
        if (head.next != null && head.next.val==head.val) head.next = null;
        return head;
    }
    
    private ListNode checkDup(ListNode node){
    	ListNode nextNode = node.next;
    	if (nextNode == null) return node;
        if (nextNode.val == node.val) {
        	ListNode newNode = nextNode.next;
        	if (newNode == null) {
        		node.next = null;
        		return node;
        	} else node.next = checkDup(newNode);
        }
       	node.next = checkDup(nextNode);
       	return node;
    }
    

    }


  • 0
    W
    This post is deleted!

Log in to reply
 

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