Striver one more question to you in the hope of getting an answer from you. How do you prepare for your videos tell us honestly.(Don't worry, no one does it better than you, just out of curiosity to know about that confidence to write that pseudocode by ourselves.i mean, how come you can come up with something like that,first you go about inserting nodes in between,in what dream will anyone understand that putting the nodes in between will allow you to then do the connect that way.How to unlock that centre of the brain.I did that O(2n) space complexity soln after actually getting hint1 on leetcode.I was happy, until i saw this)
Creating a copy means creating new nodes. In "res->random=temp->random" line, you are pointing the res->random to the temp nodes which belong to original linked list which is wrong! You need the res>random to point to the nodes of new Linked List only.
hi anyone can explain in brute force approach .. how changes made in new node called copyNode is reflected in value pair of hashmap which is cloned values ... as we assign random and next pointer to copyNode but at last we return map.get(head)?
map contains original nodes as keys & copyNodes as values... so when we do map.get(head), it means we are asking map to give us the copyNode which corrosponds to the original "head" node.
Ye sab log striver ke videos ko itna achha kyu btate rehte h like I always get confused after learning from him. If you are also facing this issue then for once try studying linked list from Fraz bhaiya. Thank me later !!!
Hate you later hona chaiye tha, pure basics se prate h striver bhaiya, basics clear ni hoyege, direct code dekho gye to confuse hi hogye. Fraz jese bs course bech rhe
because both the lists are connected to each other so to change links you have to travel through both the lists. and before moving temp you are changing links so that you're doing temp=temp.next;
In the connectRandomPointers function, we had if statement where in copyNode -> random = temp -> random -> next; I didn't understand why we took -> next here. That's the only part. Thanks .
coz we will have to point the copyNode random to copied LL random only, copy ko point karege. ->next nahi karoge then it will point to the main node which we will not be able to extract later in function 3
Another optimal approach would be clone the original nodes to new nodes ( with same random pointer ) . during cloning link next pointers of original node to clone node . Now traverse clone LL . and we can get the random link using temp.random ( goes to old node ) and temp.random.next ( goes to clone node ) . so temp.random = temp.random.next
Three days ago, i didn't even implement a linked list, and now i'm writing code without looking at pseudocode, and it runs. u r Great Striver!
u completed LL in 3 days ??
Ok money bot sycophant🤣
i never learned how to use a computer but i got l100 job at google than you striver great very very fr
mark my word -> this playlist worth is much much higher than any coding platform course;
tons of thanks;
waiting for binary tree playlist;
binary tree playlist is already there, he is not gonna make a new one
Interviewer is never happy.
😂
did it myself took 1.5 hrs for first method second is amazing
thanku striver
Amazing explanation at each and every step! There are NO other tutorials that explain every concept in such depth!🙏
Done with whole playlist. It was really amazing.
#UNDERSTOOD...Thank You So Much for this wonderful video...............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
This optimal method is freaking genius
It amazing how you can make a hard problem seem so simple and clear 👏👏👏👏👏👏👏
Bro you're explanation and methods are just mind blowing
Thankyou Striver, you nailed the explaination.
understood, best explanation on the internet
Hopefully waiting for Stack and queue playlist. Please, launch it sooner
Understood,thanks striver for this amazing video.
Good Optimal approach Broski
finished this playlist, thanks god
all A2Z ?
@@theskyraturi no i think the Linked list
Thank you so much for this invaluable explanation!,
taking a shot everytime striver says done and dusted
Perfect Explaination🙃
simply superb! expalanation
Understood. Thanks a lot ❤
Understood✅🔥🔥
Well explained ,Thank you Raj ,Love your playlist!!!
Any tentative date in mind as to when you will complete all the videos of entire sheet ? Just curious
isn't it completed already?
@@casafurix like i suppose that qs list is complete but there r a lot and lot of videos left to make for many qs
NICE SUPER EXCELLENT MOTIVATED
US
guys this one is actually a hard prob for the first time, so dont be disaappointed if you dont understand it in first attempt.
class Solution {
public Node copyRandomList(Node head){
if(head==null) return null;
if(mpp.containsKey(head))
return mpp.get(head);
Node cur=new Node(head.val);
mpp.put(head,cur);
cur.next=copyRandomList(head.next);
cur.next=copyRandomList(head.random);
return cur;
}
public Mapmpp=new HashMap();
}
🎉❤
do with o(1) sc
Done and dusted !!
please complete the stack queue playlist.
Waiting !!
UNDERSTOOD;
hats off to you.
Striver one more question to you in the hope of getting an answer from you. How do you prepare for your videos tell us honestly.(Don't worry, no one does it better than you, just out of curiosity to know about that confidence to write that pseudocode by ourselves.i mean, how come you can come up with something like that,first you go about inserting nodes in between,in what dream will anyone understand that putting the nodes in between will allow you to then do the connect that way.How to unlock that centre of the brain.I did that O(2n) space complexity soln after actually getting hint1 on leetcode.I was happy, until i saw this)
Understood, thank you.
I THINK THIS SOLUTION IS BETTER WITH O(2N) TIME COMPLEXITY
Node *copyList(Node *head) {
Node* dum = new Node(-999);
Node* res=dum;
Node* temp=head;
while(temp != NULL){
Node* n=new Node(temp->data);
res->next = n;
n->next=NULL;
res=n;
temp = temp->next;
}
temp=head;
res=dum->next;
while(temp != NULL){
res->random = temp->random;
temp=temp->next;
res=res->next;
}
return dum->next;
}
Creating a copy means creating new nodes.
In "res->random=temp->random" line, you are pointing the res->random to the temp nodes which belong to original linked list which is wrong! You need the res>random to point to the nodes of new Linked List only.
@@priyanshugupta7840 But the above code works and passes all the test cases.
@@vgodwin877 that doesn't matter. You are supposed to make a new copy , not reuse the nodes of original list. Striver solutions are correct
I think second approach is unnecessarily complicated.
understood
brute: 00:01
optimal: 12:54
thank you
Thanks
How dummyNode's next in the third step was pointing to the first node of copied LL ... as we have not updated dummyNode's next anywhere... ?
we did Node* res = dummy node so res->next = temp->next
how overall TC for the hashmap solution is O(2n)? it should be O(n) right?
1 N for the hashmap and 1 N for the new Linked list created
Understood
Understood, Just by watching the approach I was able to write the code.
hi anyone can explain in brute force approach .. how changes made in new node called copyNode is reflected in value pair of hashmap which is cloned values ... as we assign random and next pointer to copyNode but at last we return map.get(head)?
map contains original nodes as keys & copyNodes as values... so when we do map.get(head), it means we are asking map to give us the copyNode which corrosponds to the original "head" node.
😃😃
first comment
What if the same number occurs multiple times in the list?
Ye sab log striver ke videos ko itna achha kyu btate rehte h like I always get confused after learning from him. If you are also facing this issue then for once try studying linked list from Fraz bhaiya. Thank me later !!!
Hate you later hona chaiye tha, pure basics se prate h striver bhaiya, basics clear ni hoyege, direct code dekho gye to confuse hi hogye. Fraz jese bs course bech rhe
@@talkswithprabh5374 dost itna personally kyu le liya tumne mere comment ko......I was just recommending the thing which helped me 😊
I am confused why temp is moved to temp.next in last step,doesnot it make both temp and res to point at the same node after 1st traversal
no cuz we updated the temp->next to temp->next->next
because both the lists are connected to each other so to change links you have to travel through both the lists. and before moving temp you are changing links so that you're doing temp=temp.next;
Node *cloneLL(Node *head){
// Write your code here
Node *temp=head;
while(temp!=nullptr)
{
Node *naya=new Node();
naya->data=temp->data;
naya->next=temp->next;
temp->next=naya;
temp=temp->next->next;
}
temp=head;
while(temp!=nullptr)
{
if (temp->random != nullptr) {
temp->next->random = temp->random->next;
}
temp=temp->next->next;
}
temp=head;
Node* clonedHead = temp->next;
Node* clonedTemp = clonedHead;
while (temp != nullptr) {
temp->next = temp->next->next;
temp = temp->next;
if (clonedTemp->next != nullptr) {
clonedTemp->next = clonedTemp->next->next;
clonedTemp = clonedTemp->next;
}
}
return clonedHead;
}
In the connectRandomPointers function, we had if statement where in copyNode -> random = temp -> random -> next; I didn't understand why we took -> next here. That's the only part. Thanks .
coz we will have to point the copyNode random to copied LL random only, copy ko point karege. ->next nahi karoge then it will point to the main node which we will not be able to extract later in function 3
@@tanishamaheshwary9872 correct !
Time Complexity : O (2N)
Space Complexity: O (N)
Two paas solution
Node *cloneLL(Node *head){
// Write your code here
Node* temp=head;
Node* dummy=new Node(-1);
Node* res=dummy;
while(temp){
res->next=new Node(temp->data);
res=res->next;
temp=temp->next;
}
res->next=nullptr;
res=dummy->next;
temp=head;
while(temp){
res->random=temp->random;
res=res->next;
temp=temp->next;
}
return dummy->next;
}
Its not a deep copy as res->random is pointing to temp->random not the newly created list
done and dusted
respect++;
idk why but the code that striver wrote during video with different functions for everystep is showing TimeLimit Exceeded on leetcode
you shouldnt just copy the same code in leetcode.
US
us
Another optimal approach would be clone the original nodes to new nodes ( with same random pointer ) . during cloning link next pointers of original node to clone node . Now traverse clone LL . and we can get the random link using temp.random ( goes to old node ) and temp.random.next ( goes to clone node ) . so temp.random = temp.random.next
Thanks
understood
Understood
Understood