free memory to avoid memory leaking (C++)


  • 0
    X

    class Solution {
    public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
    ListNode **slow = &head;
    ListNode *fast = head;
    for (int i=1; i<n; i++)
    {
    fast=fast->next;
    }
    while (fast->next != NULL)
    {
    fast = fast->next;
    slow = &((*slow)->next);
    }
    ListNode *tmp = &(**slow);
    *slow = (*slow)->next;
    delete tmp;
    return head;
    }
    };


Log in to reply
 

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