My recursive solution on java


  • 0
    Y
    public static ListNode removeNthFromEnd(ListNode head, int n) {
       ListNode hed=head;
               if(head.next==null)
              {   head=null;
                  return head;
              }
               int cc=removeNthFromEnd2(head,n);
              if(cc>-1)
            	   hed=head.next;
             return hed;
          }
          public static int removeNthFromEnd2(ListNode head,int n)
          {  if(head==null)
              return n;
            ListNode next=head.next;
             int cc=  removeNthFromEnd2(next,n);
             cc--;
             if(cc==-1)
             head.next=head.next.next;
             return cc;
          }

Log in to reply
 

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