A JAVA DP SOLUTION (SIMILAR TO SLOW,FAST NODE)


  • 0
    R

    ...
    public class LeetCode {
    public ListNode removeNthFromEnd(ListNode head, int n) {
    ListNode pre = new ListNode(0);
    pre.next=head;
    removeNthDepth(pre,n);
    return pre.next;
    }
    private int removeNthDepth(ListNode head,int n){
    if (head==null)
    return 0;
    int depth =removeNthDepth(head.next,n);
    if (depth==n){
    head.next=head.next.next;
    }
    return depth+1;
    }
    }
    ...


Log in to reply
 

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