Skip to content

Commit effe0c7

Browse files
authored
Merge pull request #3214 from dmgobbi/fix-memory-leak
fix: Resolve memory leak in removeNthFromEnd function
2 parents 62f34be + 6059296 commit effe0c7

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

c/0019-remove-nth-node-from-end-of-list.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ struct ListNode* removeNthFromEnd(struct ListNode* head, int n){
2424
slow = slow->next;
2525
fast = fast->next;
2626
}
27-
slow->next = slow->next->next;
27+
struct ListNode* tmp = slow->next;
28+
slow->next = tmp->next;
29+
free(tmp);
2830
}
2931
else {
3032
slow->val = slow->next->val;
31-
slow->next = slow->next->next;
33+
struct ListNode* tmp = slow->next;
34+
slow->next = tmp->next;
35+
free(tmp);
3236
}
3337

3438
return head;

0 commit comments

Comments
 (0)