A recursive pointer to the pointer solution


  • 0
    F

    Note that the code below didn't actually 'delete' the node because it is not required by the problem or the test cases. It could (and should), however, be added pretty easily.

     class Solution {
        public:
            ListNode* removeNthFromEnd(ListNode* head, int n) {
                _n = n; removeAtN(&head);
                return head;
            }
        private:
            int _n;
            void removeAtN(ListNode** pp) {
                if(*pp) removeAtN(&((*pp)->next));
                if( _n-- == 0 ) *pp = (*pp)->next;
            }
        };

Log in to reply
 

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