Simple, readable and 0ms C solution


  • 0
    T
    struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
        struct ListNode *scout = head, *target = head;
        int count = 0;
        while(scout){
            scout = scout->next;
            if(count > n) target = target->next;
            count++;
        }
        if(count > n) target->next = target->next->next;
        else head = head->next;
        return head;
    }
    

    Yes, it is one pass.
    The scout pointer investigates the end of link. The target->next is the target to be removed. When count is more than n, the target pointer move and follow scout, keeping at a constant distance.


Log in to reply
 

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