1171. Remove Zero Sum Consecutive Nodes from Linked List | Prefix Sum | Brute -to- Optimal
In this video, I'll talk about how to solve Leetcode 1171. Remove Zero Sum Consecutive Nodes from Linked List | Prefix Sum | Brute to Optimal
Solution - leetcode.com/problems/remove-...
Checkout DSA-169 Series: • Aryan DSA-169 Series |...
100Days 100k Placements: • 100 Days 100K Placement
Let's Connect:
📝Linkedin: / aryan-mittal-0077
📸 Instagram: / ez.pz.dsa
📱Telegram : t.me/aryan_mittal_group
🤖 Github: github.com/aryan-0077
About Me:
I am Aryan Mittal - A Software Engineer in Goldman Sachs, Speaker, Creator & Educator. During my free time, I create programming education content on this channel & also how to use that to grow :)
✨ Timelines✨
0:00 - Important
0:46 - Problem Explanation
2:38 - Step Wise - Brute Force
6:58 - Prefix Sums for Range Sum
11:55 - Working with Pref Sum to find solution
20:04 - Dry Run
27:16 - Code & Complexity Explanation
32:26 - Problem I couldn't solve
✨ Hashtags ✨
#programming #Interviews #leetcode #faang #maang #datastructures #algorithms
Пікірлер: 17
Might be in main function they have the node address and using it. We can't see the main function, so that might be a possible reason for that
I was trying to delete the unnecessary nodes and run into the same bug too! I tried this code: class Solution { public: ListNode* removeZeroSumSublists(ListNode* head) { auto* h = head->next; delete head; return h; } }; and it gets the same error, so it maybe is that you cant touch leetcodes memory (I know this doesnt solve the problem, the code just test what happens if you delete a given node) Amazing video as always, cheers
Do let me know, if anyone knows solution of that issue in the end 🤕 !!
@jeehub041
4 ай бұрын
I guess the preexisting nodes can't be deleted only links can be broken
@neilchetty
4 ай бұрын
I think it is due to leetcode template, maybe pre-existing code for verifying the answer tries to access those listnode, but your code has already deleted it and hence it gives error
@Anonymous-lb6mc
4 ай бұрын
Hi Aryan, Found the issue!!!😃😃😃 I tried to create a copy of the given linkedlist and added the delete memory logic, hurray it worked. But using delete with the given linkedlist was throwing an error, maybe they have implemented delete for the given list, that's why we were getting error.(They might have used smart pointers in cpp that's why it deletes itself when no one is pointing towards them). I am attaching the code which is working(anyhow we are using space for creating a new list). Please let me know if my explanation is correct. class Solution { public: ListNode* removeZeroSumSublists(ListNode* head) { ListNode* dummyNode = new ListNode(0); ListNode* curr = dummyNode; while(head != nullptr){ int val = head->val; curr->next = new ListNode(val); head = head->next; curr = curr->next; } unordered_map mp; mp[0] = dummyNode; int prefSum = 0; head = dummyNode->next; while(head != nullptr){ prefSum += head->val; if(mp.find(prefSum) != mp.end()){ ListNode* start = mp[prefSum]; //start->next to head [delete all] int pf = prefSum; ListNode* temp = start->next; while(temp != head){ pf += temp->val; mp.erase(pf); ListNode* next = temp->next; delete(temp); temp = next; } start->next = head->next; }else{ mp[prefSum] = head; } head = head->next; } ListNode* newHead = dummyNode->next; delete(dummyNode); return newHead; } };
@ajayc815
4 ай бұрын
Why can't you try to free the space in the destructor ?
@chezzy6366
4 ай бұрын
@@Anonymous-lb6mc I think this may be it
i write code in python and i tried delete in python i got no errors the code is submitted succesfully, i remember for some previous problems also i deleted some nodes in the givel linked list and there also i got no errors
Shouldn't the condition on the while loop be ---> while(temp!=head), I don't understand how does the temp->next!=head is working as we might not be deleting the last prefix before head from the map in this case.
@mr_epsilon
4 ай бұрын
i didn't get that either
@ARYANMITTAL
4 ай бұрын
Thus, i told both the ways will work, Case1: temp = start here temp as soon as comes inside loop moves to next, so ideally when temp->next would have been head, before this condition only, the node before head would have been removed from map. Case2: temp = start->next while (temp != head) ……. This is what you are proposing & this also i mentioned in video & will work absolutely fine (my last code, were i am showing error, uses exact same variation)
maybe they are using those node or memory space for testcases in the driver code, so after deleting them in solution class we cant use those ListNode*s anywhere else in program
companies kyu hide kiya hai
@AzeemKhan-sn3yi
4 ай бұрын
bhai tu itna overacting kyo karta h , normal nhi h kya tu ?