Minimal Recursive Java Solution 12ms


  • 4
    	public static ListNode removeNthFromEnd(ListNode head, int n) {
    		if(head==null){
    			return null;
    		}
            int depth=removeHelper(head,n);
            head=depth>n?head:head.next;
            return head;
     
        }
    	 public static int removeHelper(ListNode head,int n){
    		if(head.next==null){
    			return 1;
    		}
    		int cur=1+removeHelper(head.next, n);
    		if(cur==n+1){
    			head.next=head.next==null?null:head.next.next;
    		}
    		return cur;
    	}
    

Log in to reply
 

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