Simple java solution


  • 2
    M
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode ret = new ListNode(0), p2 = head, prev = ret;
        ret.next = head;
        int i = 0;
        while (p2 != null){
            if (i < n){
                p2 = p2.next;
                i++;
            }
            else{
                p2 = p2.next;
                prev = head;
                head = head.next;
            }
        }
        prev.next = head.next;
        return ret.next;
    }

Log in to reply
 

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