Simple Recursive solution in 8ms using static var


  • 0
    X
       static int nk;
        class Solution {
        public:
        Solution()
        {
            nk=0;
        }
            ListNode *removeNthFromEnd(ListNode *head, int n) {
                if(head){
                head->next=removeNthFromEnd(head->next,n);
                    nk++;
                   if(nk==n)
                   {
                    return head->next;
                   }    
                }
                return head;
                
            }
        };

  • 0

    There is bug in your coding.

    Check out end of Node.

    note: End of node has no next node.

    your logic idea may miss some item of the tree.


Log in to reply
 

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