A very good tutorial, helps a lot, thank you. There is a small mistake while Zhu Qiaochu has mentioned previously. The init count value should be set to 0 because the while loop starts from the head node not head.next. Try to insert at pos 1, count == pos, this will break the loop without entering it. In this case, prev is still None, so a null pointer error will raise.
Thank you for the comment, Hao! And thank you for clarifying the mistake. Always trying to improve my content and make it better, so I appreciate the feedback! Cheers!
Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!
Actually I was so much stressed to work with data structures specially with python , then I found your videos which took me ashore and thanks a lot for all this amazing stuff.....Keep doing the great work . Hope to continue the tech journey along with you.. :D
seriously, you're the best. I was a little bit too brave and enrolled in a programming class that is waay above my league. I need to work with binary search trees for homework now. But before I am able to do THAT, I want to get behind the logic of linked lists first. You make this possible! Thank you so much
Hi Pieni. Thank you very much. Hearing things like this really makes the time and effort I put into these videos very worthwhile. Indeed, I've turned to UA-cam for many things that I felt the same way about and have benefited immensely from others who have also taken the time to teach. I wish you all the best in your class. If you feel like you've benefited from the content, feel free to subscribe and check out some of the other content I have on binary trees and binary search trees in Python. Cheers!
Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!
Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!
In function delete_node_at_pos(), count should start from 0, instead of 1, if you give count = 1and if you want to delete at position 1, then program will not go inside the while loop as count != pos. Thanks.
@@LucidProgramming actually we have to start count from 0 ...I tried and count =1 is not working...I am getting the error of "NONETYPE OBJECT has no attribute 'next'
The problem is that... If I wanna delete something at 1 then ...it won't enter into while and previous.next (previous is none) is wrong...as none doesn't have 'next' attribute
Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!
Nice tutorial! I liked it and learned easily. However, knowing which video I should follow after a video or which is the first is relatively difficult. I wish you numbered videos about each topic like Linked List 1 (Insertion), Linked List 2 (Deletion), etc. If there is a way to easily understand it please let me know by answering this comment. Thanks for great tutorial.
bro, even until now your video is still help a lot of people, really appreciate your content bro so i make sure, i didn't skip any of ads in your video. 👌👌🐱🚀
Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing! I really appreciate the support, and if you want to support the channel, I do have a PayPal link paypal.me/VincentRusso1 for donations that go directly to the creation of content on this channel. I hope that the content I provide there will enhance the videos on my UA-cam page. bit.ly/lp_email
thank you for the great tutorial!! helped me a lot with my understanding and coding of the linked list. flunked my test because linked list had a great weightage on the exam and after watching this, i could code it well without even referring to the codes over and over. thank you so much
Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!
Excellent tutorial Thanks! I've been searching for data structures written in python but all I found are written in other languages or explained as algorithms Slight correction at 14:32 the count should be set to zero. As I am new to ds, it took me some time and a lot of frustration to correct it myself checking and rechecking the code again and again 😅 Please correct it in the comments and pin it so that others don't go through the same pain
Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!
Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!
As @LucidProgramming mentioned that before that only he kept the 0 value to Cur or to head value, So he started of with count = 1, in the case were pos is not equal to head.
Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing! I really appreciate the support, and if you want to support the channel, I do have a PayPal link paypal.me/VincentRusso1 for donations that go directly to the creation of content on this channel. I hope that the content I provide there will enhance the videos on my UA-cam page. bit.ly/lp_email
14:33 I think count should start at 0 if the first node is gonna be the head. Otherwise you get an off by one error (imagine a list with a single node, and you're asked to remove element number 1). I'm assuming the head is at position 0.
@@lukpisimoh Not at all, in fact, the quality of these videos is directly tied to my great audience and their feedback. The code on my GitHub does have this updated change. Thank you for posting the comment, and thank you for taking the time to mention that here. I appreciate your viewership and support!
Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing! I really appreciate the support, and if you want to support the channel, I do have a PayPal link paypal.me/VincentRusso1 for donations that go directly to the creation of content on this channel. I hope that the content I provide there will enhance the videos on my UA-cam page. bit.ly/lp_email
Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!
Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing! I really appreciate the support. I hope that the content I provide there will enhance the videos on my UA-cam page. bit.ly/lp_email
I have a query!! While deleting a node, shouldn't we delete the object itself instead of setting it to None in order to free the memory space? By the way, I really appreciate the effort you put into these videos. Have helped me a lot in my learning process.
Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing! I really appreciate the support, and if you want to support the channel, I do have a PayPal link (www.paypal.me/VincentRusso1) for donations that go directly to the creation of content on this channel. I hope that the content I provide there will enhance the videos on my UA-cam page. bit.ly/lp_email
Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing! I really appreciate the support, and if you want to support the channel, I do have a PayPal link paypal.me/VincentRusso1 for donations that go directly to the creation of content on this channel. I hope that the content I provide there will enhance the videos on my UA-cam page. bit.ly/lp_email
@@rohithreddysureddy9151 Ah gotcha. I believe changing "count = 1" to "count = 0" solves the problem. I updated the code on my Github as well to reflect this. Thanks for pointing that out!
Thanks a lot vinnie.. Superb teaching style and great videos..keep going .. Wish u would do more videos especially on time efficiency and space efficiency and complete data structures..
Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing! I really appreciate the support, and if you want to support the channel, I do have a PayPal link www.paypal.me/VincentRusso1 for donations that go directly to the creation of content on this channel. I hope that the content I provide there will enhance the videos on my UA-cam page. bit.ly/lp_email
@@LucidProgramming yes i tried this method def deleteNode(node_ptr): temp = node_ptr.next node_ptr.data = temp.data node_ptr.next = temp.next but using this method we cant delete last node. btw thanks for replying:)
At 13:00 don't we also need to check if the linked list isn't empty altogether? If it is empty then I think an error will be raised saying None(object) which is self.head has no attribute next
I agree that we need a different condition to remove the first element different from the logic for middle elements. But what i actually meant was: In the line, self.head = current_node.next it would give a error if the linked list was empty as current_node is self.head and in a empty list self.head is not assigned to any "node object" and thus does not have a next attribute. So in the if condition, rather than, If pos==0: #now I think it should be If pos==0 and self.head != None: I am sorry for making it so long, hope I cleared my question.
Great content. However, a test of llist.delete_node_at_pos(1) will fail the code. Because the current code doesn't cover the case of count==pos right off the bat. A minor change to the while loop would fix it: while True: prev = cur_node cur_node = cur_node.next if cur_node is None or count == pos: break count += 1
Hey Yongguang. Setting the variable to None is precisely the same idea as setting the variable to "null" when implementing a linked list in language like C++ or Java. When implementing these things in C++/Java, you want to make sure that you do indeed set the nodes to null when they stop pointing to other nodes in the list so as to avoid "dangling pointers". It's kind of the same idea here, although most likely nothing catastrophic will happen if you don't, it's just good practice. Hope that makes sense, and thanks for watching!
Is the cur node a local variable that will be automatically collected after the method return? Is it only a ritual code here to set it to None? Please help clarify.
Can someone explain the part on def delete(self, key): cur_node = self.head if cur_node and cur_node.data == key (what does this mean? the current node data is equal to the key we seek?) self.head = cur.node.next cur_node = None
@@vayunandu I tried solving it for duplicates and this is what I came up with: 🙂 def remove(self, key): remove_count = 0 while self.head.data == key and self.head: remove_count += 1 self.head = self.head.next if not self.head: return f'node {key} was removed {remove_count} times list is empty' prev_node = self.head cur_node = prev_node.next while cur_node: if cur_node.data == key: prev_node.next = cur_node.next cur_node = None remove_count += 1 else: prev_node = cur_node cur_node = prev_node.next return f'node {key} was removed {remove_count} times'
LucidProgramming actually we have to start count from 0 ...I tried and count =1 is not working...I am getting the error of "NONETYPE OBJECT has no attribute 'next'
The problem is that... If I wanna delete something at 1 then ...it won't enter into while and previous.next (previous is none) is none...and none doesn't have 'next' attribute
@@LucidProgramming when you try to delete "C" or "D" , it is still deleting only "B" for some reason. I have the same issue and i am not understanding the reason behind it
Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing! I really appreciate the support, and if you want to support the channel, I do have a PayPal link paypal.me/VincentRusso1 for donations that go directly to the creation of content on this channel. I hope that the content I provide there will enhance the videos on my UA-cam page. bit.ly/lp_email
Hi Zhu. Since we explicitly check for pos = 0 prior to running our loop, we already explicitly take care of this case. Since the remainder of the code goes from node 1 on, starting the count at 1 appears to be okay, unless I'm missing something.
when deleting a node that is not the head, can't you just point the prev node to the next.next? why do you care about setting the deleted node next to None?
delete node at position is not working because when the position is 1, both while loop and if conditional don't run and prevNode is still None, and prevNode.next (next of None is not valid). the following code works def delNodePosition(self, position): currentNode = self.head if(position == 0): self.head = currentNode.next currentNode = None return count = 1 prevNode = currentNode currentNode = currentNode.next while(currentNode and count != position): prevNode = currentNode currentNode = currentNode.next count += 1 if currentNode is None: return prevNode.next = currentNode.next
Someone helppp: def del_pos(self, data): curr_node=self.head for i in range(0,data): curr_node=curr_node.next prev_node=None for i in range(0,data-1): prev_node=prev_node.next prev_node.next=curr_node.next curr_node.next=None
Hi Ilya. Thanks for the comment. Indeed, this code isn't meant to necessarily scale particularly well. There are lots of off the shelf and built-in solutions for Python already that handle this task much better than this code. The only reason I have it here is that linked lists are a fundamental concept for computer science and often technical interviews. Understanding the inner workings of these things can be helpful, that was my only intent. Good point though!
A very good tutorial, helps a lot, thank you. There is a small mistake while Zhu Qiaochu has mentioned previously.
The init count value should be set to 0 because the while loop starts from the head node not head.next.
Try to insert at pos 1, count == pos, this will break the loop without entering it.
In this case, prev is still None, so a null pointer error will raise.
Thank you for the comment, Hao! And thank you for clarifying the mistake. Always trying to improve my content and make it better, so I appreciate the feedback! Cheers!
yea setting count = 0 is much better than,
prev = self.head
cur_node = self.head.next
count = 1
which I did.. :P
Thanks, man, I couldn't be able to understand why this is not working.
The previous video's explanation was so good that I came up with the delete function on my own. Great channel!
Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!
Actually I was so much stressed to work with data structures specially with python , then I found your videos which took me ashore and thanks a lot for all this amazing stuff.....Keep doing the great work . Hope to continue the tech journey along with you.. :D
Happy to hear that, thanks for the kind words and best of luck to you in your journey!
seriously, you're the best. I was a little bit too brave and enrolled in a programming class that is waay above my league. I need to work with binary search trees for homework now. But before I am able to do THAT, I want to get behind the logic of linked lists first. You make this possible! Thank you so much
Hi Pieni. Thank you very much. Hearing things like this really makes the time and effort I put into these videos very worthwhile.
Indeed, I've turned to UA-cam for many things that I felt the same way about and have benefited immensely from others who have also taken the time to teach.
I wish you all the best in your class. If you feel like you've benefited from the content, feel free to subscribe and check out some of the other content I have on binary trees and binary search trees in Python. Cheers!
I have been looking for linked list tutorials in python and also read some blogs on it but your videos helped me the most.
Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!
You are a GOD! Really helped me out in my Data Structures class since i'm doing it in Python instead of java
Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!
In function delete_node_at_pos(), count should start from 0, instead of 1,
if you give count = 1and if you want to delete at position 1, then program will not go inside the while loop as count != pos.
Thanks.
Doesn't the first "if" statement above this account for that scenario?
@@LucidProgramming actually we have to start count from 0 ...I tried and count =1 is not working...I am getting the error of "NONETYPE OBJECT has no attribute 'next'
The problem is that... If I wanna delete something at 1 then ...it won't enter into while and previous.next (previous is none) is wrong...as none doesn't have 'next' attribute
In function delete_node_at_pos(), the problem is not with count Instead change prev =None to prev = self.head
thanks!!
@@thilakreddy13 Thank you so much! I had a hard time figuring out the problem with this NoneType error
This video deserves way lot of views than it is!
Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!
@@LucidProgramming done brother 😊
@@geekyprogrammer4831 Very much appreciated! Thank you!
Nice tutorial! I liked it and learned easily. However, knowing which video I should follow after a video or which is the first is relatively difficult. I wish you numbered videos about each topic like Linked List 1 (Insertion), Linked List 2 (Deletion), etc. If there is a way to easily understand it please let me know by answering this comment. Thanks for great tutorial.
bro, even until now your video is still help a lot of people, really appreciate your content bro so i make sure, i didn't skip any of ads in your video. 👌👌🐱🚀
Very much appreciated. From one Vincent to another, thank you!
Superb explaination ......
TQ so much...
Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing!
I really appreciate the support, and if you want to support the channel, I do have a PayPal link
paypal.me/VincentRusso1
for donations that go directly to the creation of content on this channel.
I hope that the content I provide there will enhance the videos on my UA-cam page.
bit.ly/lp_email
thank you for the great tutorial!! helped me a lot with my understanding and coding of the linked list. flunked my test because linked list had a great weightage on the exam and after watching this, i could code it well without even referring to the codes over and over. thank you so much
Really glad to hear the video helped. Sometimes some extra practice and some visuals help to solidify things. Thanks for the comment and for watching!
very clean explanations, brilliant
Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!
Excellent tutorial
Thanks! I've been searching for data structures written in python but all I found are written in other languages or explained as algorithms
Slight correction at 14:32 the count should be set to zero. As I am new to ds, it took me some time and a lot of frustration to correct it myself checking and rechecking the code again and again 😅
Please correct it in the comments and pin it so that others don't go through the same pain
Nice! I believe this is updated on my GitHub code. Thanks for pointing this out in any case!
Your videos are really helpful. Amazing work!
Cheers, and thanks for watching!
Dude you are an absolute life saver, your explanations are very clear and concise, and they actually make sense. Keep it up!
Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!
@@LucidProgramming yes agreed to bayan
we want more DS
@@mightyprogrammer2899 Thank you both for the encouragement! I hope that I can find the time to put out some new videos!
TY! great tutorials on linked lists. Using these as study guide for my technical interview
Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!
i too observed count should start from 0. by the way very good series . i am planning to do all the videos in coming days
Good luck on your journey with the videos! I hope you enjoy the process! :)
As @LucidProgramming mentioned that before that only he kept the 0 value to Cur or to head value, So he started of with count = 1, in the case were pos is not equal to head.
You have the best videos
Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing!
I really appreciate the support, and if you want to support the channel, I do have a PayPal link
paypal.me/VincentRusso1
for donations that go directly to the creation of content on this channel.
I hope that the content I provide there will enhance the videos on my UA-cam page.
bit.ly/lp_email
14:33 I think count should start at 0 if the first node is gonna be the head. Otherwise you get an off by one error (imagine a list with a single node, and you're asked to remove element number 1). I'm assuming the head is at position 0.
The videos are great btw, I hope I didn't come off cocky, I was just trying to help (and clarify my doubts)
@@lukpisimoh Not at all, in fact, the quality of these videos is directly tied to my great audience and their feedback. The code on my GitHub does have this updated change. Thank you for posting the comment, and thank you for taking the time to mention that here. I appreciate your viewership and support!
Just marvellous
Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing!
I really appreciate the support, and if you want to support the channel, I do have a PayPal link
paypal.me/VincentRusso1
for donations that go directly to the creation of content on this channel.
I hope that the content I provide there will enhance the videos on my UA-cam page.
bit.ly/lp_email
Great explanation
Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!
Thanks bro it helped me a lot Genuinely nice content. Instead of reading 100 of blogs in internet. Just check it out :D
Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing! I really appreciate the support. I hope that the content I provide there will enhance the videos on my UA-cam page.
bit.ly/lp_email
I have a query!! While deleting a node, shouldn't we delete the object itself instead of setting it to None in order to free the memory space?
By the way, I really appreciate the effort you put into these videos. Have helped me a lot in my learning process.
It depends on the requirements. One could consider deleting the object or setting to None depending on what is asked and what the use case is.
Thank you for answering.
@@b_sobhan_ Of course, thank you for watching.
Great explanation!
Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing!
I really appreciate the support, and if you want to support the channel, I do have a PayPal link (www.paypal.me/VincentRusso1) for donations that go directly to the creation of content on this channel.
I hope that the content I provide there will enhance the videos on my UA-cam page.
bit.ly/lp_email
Excellent! sir, please give us the series of "Data structures using Recursion"
Thank you! I do indeed have a Recursion playlist here: bit.ly/lp_recursion
what if the node we want to remove is the last one,? because lastnode.next will also going to be none please explain
Great Tutorial , thanks a lot .
Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing!
I really appreciate the support, and if you want to support the channel, I do have a PayPal link
paypal.me/VincentRusso1
for donations that go directly to the creation of content on this channel.
I hope that the content I provide there will enhance the videos on my UA-cam page.
bit.ly/lp_email
what about if list is empty and you try to delete at pos. i don't see a check that head is not none. does that matter?
del_at_position is not working for '1' position.But,its working fine for others.Plz clarify.
It's working fine for me. Could you provide a complete example?
Tried to execute your github code with delete_node_at_pos(1). Its throwing a attribute error: ‘NoneType’ object has no attribute ‘next’.
@@rohithreddysureddy9151 Ah gotcha. I believe changing "count = 1" to "count = 0" solves the problem. I updated the code on my Github as well to reflect this. Thanks for pointing that out!
Thanks a lot vinnie.. Superb teaching style and great videos..keep going ..
Wish u would do more videos especially on time efficiency and space efficiency and complete data structures..
@@rohithreddysureddy9151 Thank you, Rohith! I hope to continue making more videos on this channel. Thank you for your encouragement and support!
Thank you very much.
No problem, hope it was helpful! :)
Was very helpful. Thankyou :)
And yes...count should start from 0 because if its started with 1, for pos = 1, the prev = None and therefore has no attribute "next"
Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing!
I really appreciate the support, and if you want to support the channel, I do have a PayPal link www.paypal.me/VincentRusso1
for donations that go directly to the creation of content on this channel.
I hope that the content I provide there will enhance the videos on my UA-cam page.
bit.ly/lp_email
Thanks. This helps me learn. May I know how to delete from the tail? I always get Nonetype object has no attribute next.
No problem, I hope the video was helpful. If you know the length, then you could just delete it at that position using the function we wrote.
Sir, i have one question
Delete the last node from linked list without head pointer.
how can we do this?
i mean is this even possible to do?
Have you tried anything to solve this problem?
@@LucidProgramming yes i tried this method
def deleteNode(node_ptr):
temp = node_ptr.next
node_ptr.data = temp.data
node_ptr.next = temp.next
but using this method we cant delete last node.
btw thanks for replying:)
@@shreyaskadoor3582 Couldn't you add an if statement to check for the last node condition?
At 13:00 don't we also need to check if the linked list isn't empty altogether? If it is empty then I think an error will be raised saying None(object) which is self.head has no attribute next
btw great video
Wouldn't it be a bit more helpful to the user to explicitly cover this case though?
@@KshitijKale And thank you! :)
I agree that we need a different condition to remove the first element different from the logic for middle elements. But what i actually meant was:
In the line, self.head = current_node.next it would give a error if the linked list was empty as current_node is self.head and in a empty list self.head is not assigned to any "node object" and thus does not have a next attribute.
So in the if condition, rather than,
If pos==0: #now
I think it should be
If pos==0 and self.head != None:
I am sorry for making it so long, hope I cleared my question.
@@KshitijKale Oh okay, gotcha. Yeah, I think that makes sense to me!
Great content. However, a test of llist.delete_node_at_pos(1) will fail the code. Because the current code doesn't cover the case of count==pos right off the bat. A minor change to the while loop would fix it:
while True:
prev = cur_node
cur_node = cur_node.next
if cur_node is None or count == pos:
break
count += 1
Cool, feel free to make a pull request to improve the GitHub code in the description! Cheers!
Great tutorial! Would you mine explaining a bit more what “self” means! I get the code, but I want to understand what self is.
"self" is a keyword that refers to the instance of the class. I would suggest Googling around for "classes in Python" for further info.
LucidProgramming ahh ok gotcha! Will do!
@@rumikinss9125 I offer one-on-one consulting for Python, so if you want to book a session, I'd be happy to go into more detail for you!
LucidProgramming thanks for the offer, but im a broke college student!
@@rumikinss9125 No worries, I appreciate the support and viewership :)
What does setting cur_node to None accomplish? Does it deallocate memory taken up by the deleted node?
Hey Yongguang. Setting the variable to None is precisely the same idea as setting the variable to "null" when implementing a linked list in language like C++ or Java. When implementing these things in C++/Java, you want to make sure that you do indeed set the nodes to null when they stop pointing to other nodes in the list so as to avoid "dangling pointers". It's kind of the same idea here, although most likely nothing catastrophic will happen if you don't, it's just good practice. Hope that makes sense, and thanks for watching!
Make sense. Thank you so much for reply!
Is the cur node a local variable that will be automatically collected after the method return? Is it only a ritual code here to set it to None? Please help clarify.
Can someone explain the part on
def delete(self, key):
cur_node = self.head
if cur_node and cur_node.data == key (what does this mean? the current node data is equal to the key we seek?)
self.head = cur.node.next
cur_node = None
If the current node exists and the data held at that node is equal to the key.
can I get the link to the ppt which you used? btw great video thanks
Updated the description with a link to the slides. Cheers, and thanks for watching!
@@LucidProgramming thanks
@@LucidProgramming great series bro please upload more like backtracking bfs dfs or many sorting algos
@@subhambanerjee6334 It's on my to-do list! Thanks for watching! :)
Thank you very much. But it won't delete if the linked list has duplicates though. :)
You're welcome, and you're right. Not sure if I mentioned that I assumed the list entries were unique. Sorry about that!
@@LucidProgramming You did mention that. I was excited to try it on my own. I didn't look at that. Sorry about that! Thank you!
@@vayunandu No worries, thanks for the comment all the same!
@@vayunandu I tried solving it for duplicates and this is what I came up with: 🙂
def remove(self, key):
remove_count = 0
while self.head.data == key and self.head:
remove_count += 1
self.head = self.head.next
if not self.head:
return f'node {key} was removed {remove_count} times
list is empty'
prev_node = self.head
cur_node = prev_node.next
while cur_node:
if cur_node.data == key:
prev_node.next = cur_node.next
cur_node = None
remove_count += 1
else:
prev_node = cur_node
cur_node = prev_node.next
return f'node {key} was removed {remove_count} times'
Initial assigning of count will be Zero not One
Count=0 ✓
Count=1×
Then only it iterates the loop.
Have you run the code on my GitHub? Setting `count=1` works as expected.
LucidProgramming actually we have to start count from 0 ...I tried and count =1 is not working...I am getting the error of "NONETYPE OBJECT has no attribute 'next'
The problem is that... If I wanna delete something at 1 then ...it won't enter into while and previous.next (previous is none) is none...and none doesn't have 'next' attribute
Are you referring to a specific spot in the video? Are you sure your code is consistent with my code on GitHub?
@@LucidProgramming at 14:31 you initialized count by 1 but it should be 0
@@lol-ki5pd Ah, that's correct. I must have altered that on my GitHub because the code there is correct. Thanks for catching that.
@@LucidProgramming no problem :) btw you are the best teacher I ever had
deleting at given position is not working correctly plx help
How? You haven't given me any more information so I can't help you.
@@LucidProgramming when you try to delete "C" or "D" , it is still deleting only "B" for some reason. I have the same issue and i am not understanding the reason behind it
@@akhilvarma1 Are you sure you're running the right code? I just ran the code on my GitHub and it works as expected.
Amazing😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍
Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing!
I really appreciate the support, and if you want to support the channel, I do have a PayPal link
paypal.me/VincentRusso1
for donations that go directly to the creation of content on this channel.
I hope that the content I provide there will enhance the videos on my UA-cam page.
bit.ly/lp_email
Initial value of count must be 0
Hi Zhu. Since we explicitly check for pos = 0 prior to running our loop, we already explicitly take care of this case. Since the remainder of the code goes from node 1 on, starting the count at 1 appears to be okay, unless I'm missing something.
yes, try run the code, you will see. Your work is pretty awesome. I like the videos!!
Hmm, I will indeed follow-up on that. Also, thank you very much for your kind comment, I'm happy to hear that you like the content :)
Hi. Zhu Qiaochu is right. Count should be initialized to 0 because the cur_node starts from the head node(0th position).
if pos == 1 the while loop will not be entered. thus the head is cur node which will be deleted. error
finally after hours 1 like
Thanks, Kelvin. I appreciate the like! Cheers, and thanks for watching.
when deleting a node that is not the head, can't you just point the prev node to the next.next? why do you care about setting the deleted node next to None?
That's more of an artifact of languages like "C" that would prevent things like "dangling pointers".
The one dislike is from a person who died million years back... :P:P
delete node at position is not working because when the position is 1, both while loop and if conditional don't run and prevNode is still None, and prevNode.next (next of None is not valid).
the following code works
def delNodePosition(self, position):
currentNode = self.head
if(position == 0):
self.head = currentNode.next
currentNode = None
return
count = 1
prevNode = currentNode
currentNode = currentNode.next
while(currentNode and count != position):
prevNode = currentNode
currentNode = currentNode.next
count += 1
if currentNode is None:
return
prevNode.next = currentNode.next
Thanks for sharing the code!
count from 1 wont work it should be 0
Seems to work for me.
Someone helppp:
def del_pos(self, data):
curr_node=self.head
for i in range(0,data):
curr_node=curr_node.next
prev_node=None
for i in range(0,data-1):
prev_node=prev_node.next
prev_node.next=curr_node.next
curr_node.next=None
I would encourage you to pepper in some print statements to see why your code might not be working as expected.
This is good content, but man do you speak too fast for me to grasp everything!
The beauty of UA-cam is that you can replay and control the speed. Thanks for watching, and glad to hear it was broadly useful to you!
@@LucidProgramming Getting accustomised gradually as I absorb the material deeply. Thanks for the videos!
Ok, add 1000000 items and you'll see that its shit code.
Hi Ilya. Thanks for the comment. Indeed, this code isn't meant to necessarily scale particularly well. There are lots of off the shelf and built-in solutions for Python already that handle this task much better than this code. The only reason I have it here is that linked lists are a fundamental concept for computer science and often technical interviews. Understanding the inner workings of these things can be helpful, that was my only intent. Good point though!