Anybody tried to use a recursive method?


  • 0
    A

    As following:

        int traverseList(ListNode *head, int n){
            if(head == nullptr){
                return 0;
            }else{
                int ret = traverseList(head->next, n);
                if(ret == n){
                    head->next = (head->next)->next;
                }
                return ret + 1;
            }
        }
    
    
        ListNode *removeNthFromEnd(ListNode *head, int n) {
    
            int ret = traverseList(head, n);
            if(ret == n){
                head = head->next;
            }
            return head;
        }

Log in to reply
 

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