Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Divyanshu Varma
Solution does not delete the bypassed nodes

Divyanshu Varma

Apr 25, 2025

/* * We should delete unused bypassed nodes otherwise * if you had n nodes to begin with and deleted m nodes * you will still be consuming n nodes worth of memory. */ ListNode* deleteDuplicates(ListNode* head) { if(!head or !head->next) { // base case return head; } auto curr = head, ahead = head->next; // adjacent nodes while(ahead) { if(curr->val == ahead->val) { auto temp = ahead; curr->next = ahead->next; // skip duplicate ahead = ahead->next; delete temp; // delete unused node } else { curr = curr->next; ahead = ahead->next; } } return head; }

0

0

Comments
Comments

On this page