My Java Solution


  • 11
    V
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode voidHead = new ListNode(-1);
        voidHead.next = head;
        ListNode p1 = voidHead;
        ListNode p2 = voidHead;
        while (p1.next!=null){
            p1=p1.next;
            if (n--<=0)p2=p2.next;
        }
        if (p2.next!=null) p2.next=p2.next.next;
        return voidHead.next;
    }

Log in to reply
 

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