Simple C++ solution swaps and avoids memory leak


  • 1
    D
    class Solution {
    public:
        void deleteNode(ListNode* node) {
            std::swap(node->val, node->next->val);
            auto next = node->next;
            node->next = node->next->next;
            delete next;
        }
    };

Log in to reply
 

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