Stupid but short C++ solution

  • 0

    9 sloc is enough if the deletion is not taken care of, which should not be a huge problem if this function is intended to be called only once.

    class Solution {
        ListNode* removeNthFromEnd(ListNode* head, int n) {
            std::vector<ListNode **> ptrs;
            ListNode **curr = &head;
            while (*curr) {
                curr = &(*curr)->next;
            auto it = ptrs.end() - n;
            ListNode *deletee = **it;
            **it = (**it)->next;
            delete deletee;
            return *ptrs.front();


Log in to reply

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