Python Data Structures #2: Linked List

Поділитися
Вставка
  • Опубліковано 24 сер 2024

КОМЕНТАРІ • 344

  • @Noooname945
    @Noooname945 6 років тому +65

    @Brian Faure. Thanks for your tutorial. I really appreciate for your efforts to make this tutorial. After watching your vedio , I am confused by one thing. The append function doesn't show the great advantage of the linked list. Could you give us more explanation about this. I think one great merit for linked list is: when we want to insert a node into the list, it just need to change the pointer instead of moving the items in the list to create an gap and then inserting the new item into the list. In the linked list, when we want to insert an node, we just need to change two pointers, which helps us save time, especially the list contains millions of node. But your append function, it needs to iterate to find the last node and then add a new node. This doesn't show the virtue of the linked list. So I am think about the append function whether we could just add the new node at the end of the linked list without iteration. Thanks for your help.

    • @BrianFaure1
      @BrianFaure1  6 років тому +51

      Hi Xiufeng, thanks for the nice words! Yes the way the append function works currently is O(n), and is not the most efficient. By adding in an extra class member variable, we can call 'tail' for example, we can reduce the complexity of the append function down to constant time, or O(1). A possible implementation could be as follows:
      > def append(self,data):
      > new_node=node(data)
      > self.tail.next=new_node
      > self.tail=new_node
      Take note that for this to work, we will need to declare this 'tail' variable in the constructor of the class:
      > def __init__(self):
      > self.head=node()
      > self.tail=self.head
      If you wish to take this approach, you'll also need to be careful to set the 'tail' correctly inside of the 'erase' function (for example, if you try to erase the last element in the list, you'll need to set the tail to the _new_ last element). The following 'erase' function includes a single 'if' statement which should achieve this:
      > def erase(self,index):
      > if index>=self.length() or index print "ERROR: 'Erase' Index out of range!"
      > return
      > cur_idx=0
      > cur_node=self.head
      > while True:
      > last_node=cur_node
      > cur_node=cur_node.next
      > if cur_idx==index:
      > last_node.next=cur_node.next
      > if last_node.next==None: self.tail=last_node ## *Here is where we set the tail*
      > return
      > cur_idx+=1
      I've done some simple tests with this new implementation and it seems to be working, but feel free to let me know if you find any issues or have any other questions! I'm going to pin this comment so any others with the same question can see it.

    • @hoangminhnguyen2191
      @hoangminhnguyen2191 5 років тому +1

      Hello, I have a question about the way you append new object with self.tail: why setting both self.tail and self.tail.next equal to next_node. Does this mean you set the last element (after the tail) equal to the new node and then set the tail of the list to that node?

    • @skillfulactor09
      @skillfulactor09 5 років тому

      Can you make more I love yours do you have stuff on graphs or heaps or merge sort

    • @JF-di5el
      @JF-di5el 5 років тому +4

      So Chinglish

  • @domss1174
    @domss1174 6 років тому +184

    3:40 node class, 5:00 linked list class, 6:12 linked list-APPEND method, 7:55 linked list-get LENGTH method, 9:22 linked list-DISPLAY LIST method, 11:31 linked list-get DATA method, 14:50 linked list ERASE method

    • @AbdulSamad-qv4tr
      @AbdulSamad-qv4tr 3 роки тому

      THANK YOU

    • @seanli75
      @seanli75 2 роки тому +1

      Thanks for the timestamps man! It was really helpful!

  • @user-eq9zo5vj7c
    @user-eq9zo5vj7c 6 місяців тому +9

    I struggled a lot with DSA until I found this channel. Now I struggle a lot less thanks to you.

  • @code4code857
    @code4code857 6 років тому +45

    I was a beginner and struggled a lot for the right content and pace which suited me. I have started watching your videos and I must say the concepts are taught very well. At most places I see people just giving presentation through slides and your videos teach us how to implement them. My request to you is to please put more videos in this very playlist covering topics such as Heaps, Recursion etc . Basically the topics which you have'nt uploaded here. You're doing a great job sir. Your efforts can make someone's career. Someone like me. Thank you and have a nice day !

  • @MCMB29
    @MCMB29 2 роки тому +11

    5 years later and this is still a great video! Awesome in-depth explanation.

  • @Julia-rq7uj
    @Julia-rq7uj 6 років тому +14

    i must say, this is the simplest coding of linked list i've seen so far on the internet

    • @sauravraj1085
      @sauravraj1085 3 роки тому

      Lolll, this is the most general and common code for linked list.

  • @gabrieldewraj3502
    @gabrieldewraj3502 3 роки тому +5

    This man deserves an award for how well he broke down every concept!

  • @austinbao
    @austinbao 3 роки тому +2

    I have searched through UA-cam to find someone to explain Linked lIsts and I gotta say, your video was the most helpful. Thank you so much!

    • @BrianFaure1
      @BrianFaure1  3 роки тому

      Glad to hear it helped you, thanks for watching Austin!

  • @ETR12935
    @ETR12935 Місяць тому +1

    First dsa video! the keyboard sound was just 🤩

  • @softwareengineer8923
    @softwareengineer8923 11 місяців тому +2

    Your explanation is too clean and lucid.Thanks for a great video!

  • @tguigz
    @tguigz 5 років тому +6

    I know this is more geared to beginners but a better way to do length for an object is by levying your own dunder method or "magic method". You can do this by defining your function as: def __len__(self): this will let you leverage the length of your linked-list by using len(your_linked_list) rather than using your_linked_list.length(), which will be more pythonic and user friendly. You could also use def __getitem__(): for yor get function.

  • @djlazer1
    @djlazer1 Рік тому +1

    I love you dude thanks for explaining this stuff so clearly. So many other channels explain this stuff so badly, but you make it so easy to understand.

  • @yunsizhang3847
    @yunsizhang3847 2 роки тому +3

    Agree. The current appending solution is O(n) not O(1). Your solution should be O(1), which is the benefits of using Linked List
    .

  • @mouseen92
    @mouseen92 6 років тому +3

    Excellent video, really clear, direct, concise and easy to follow. Amazing, so many people on UA-cam need to learn a thing or two from you.

  • @MrBloodshadow323
    @MrBloodshadow323 Рік тому

    I'm a beginner at coding and this was very helpful. I also liked the cadence of your voice, it helped me from not zoning out. Keep it up!

  • @alexlencz7346
    @alexlencz7346 5 років тому +7

    Extremely helpful tutorial! Clear, concise presentation. And Python is also my favorite language!

  • @zulfiqarali1212
    @zulfiqarali1212 6 років тому +12

    beleive me you are one of the greatest teacher of coding. oh its true ,,,its dammmnnn true... keep it up.

  • @yajingli1990
    @yajingli1990 5 років тому +4

    Thank you for making this great tutorial, it's very helpful! I've been struggling with understanding the linked list structure and finally find answers from your video, thank you very much!

  • @patrickmutuku9579
    @patrickmutuku9579 6 років тому +3

    I love your tutorials. I wish I could have discovered them before starting CS110 three months ago. Keep up the great work

  • @apalsnerg
    @apalsnerg 5 місяців тому

    Thank you so much for this! This is just what I needed to understand linked lists properly!

  • @C-Swede
    @C-Swede 6 років тому

    You're a great tutor! This feels like it brings me closer to where Python will finally "click" with me.

  • @mr-engin3er
    @mr-engin3er 3 роки тому

    I searched for python linked list and watch many tutorials but I found this tutorial is best.
    expect more python tutorials from this channel.

  • @jpchato
    @jpchato 3 роки тому

    my_list = LinkedList()
    my_list.append('3 years later but still great, thanks Brian')
    my_list.display()

  • @sanjiblamichhane
    @sanjiblamichhane 2 роки тому +4

    Good job making this video.
    - append() method and elems.append() might be confusing if you create a method to insert at end with name other than append().

  • @varunnarayanan6301
    @varunnarayanan6301 2 роки тому

    Its 2022 still the best linked list video on python

  • @darksoul.0x7
    @darksoul.0x7 6 років тому +6

    I have an error on display function it's say that 'None type' object has no attribute 'data'

  • @EugeneMillerErm
    @EugeneMillerErm 3 роки тому +2

    I found the concept interesting, I'm not sure where I'd use it over a list. I must say that your code can be made much more efficient by adding a _length variable and incrementing it when you append and de-incrementing it when you erase. No need to iterate when you can store it in an integer. Also __repr__() could be used vs display or perhaps a __str__(). so you could just print(linked_list) __getitem__() or perhaps __get__() could be used so you could linked_list[0] magic methods are kinda cool.

  • @charlielin188
    @charlielin188 4 роки тому +1

    Thank u for making this video, neat code and clear explanation, 10/10. I really hope one day I can be as intelligent as you are.

  • @zwj808
    @zwj808 5 років тому

    Thank you for making such a great tutorial! It really helps me aces the linked list, which I kept avoiding before. Finally found your clear step by step tutorial! It is so cool and excellent, thank you!

  • @nackyding
    @nackyding 5 років тому

    Thanks needed a quick refresher on linked list. Going to see if you have double linked list now...thanks again

  • @bhavishahadiyal7836
    @bhavishahadiyal7836 Рік тому

    Best explanation ever please never stop making great videos, we will subscribe

  • @collintech057
    @collintech057 2 роки тому

    Thank You Brian. this video was very helpful. i watched other tutorials and they were confusing. one thing I discovered about algorithm and data structure is that the teacher's use of plain english and well outlined variable is fundamental to understanding the concept easily. i grasped everything you taught in this video but the challenge i face is that once i try to implement it on my own, i run into errors because i don't like the idea of copying people's code rather than either interpreting in a paper or typing them on my own. i have fundamental knowledge of programing and am a web developer. i want you to help me master OOP, DSA and Iterations. Thank You

  • @QVL75
    @QVL75 3 роки тому +1

    I love your tutorial video. Excellent presentation. Very clear explanation. 2 thumbs up.

  • @jordonmarchesano9219
    @jordonmarchesano9219 2 роки тому

    awesome man thank you. Been having trouble with data structures but this seriously helped a lot.

  • @Abhishek-fe3zs
    @Abhishek-fe3zs Рік тому

    Your channel is gold

  • @SKEEBWALK4k
    @SKEEBWALK4k 2 роки тому

    i just wasted 2 hours too complete this get method finally thanks

  • @intisharalammisbahul9927
    @intisharalammisbahul9927 3 роки тому

    I looked for copper but found diamond. Loved the video, will help me with my exam.

  • @grifmang
    @grifmang 3 роки тому +1

    Just wanted to add that the Doubly Linked List also has access to the last node, or the tail. Where the Singly Linked List doesn't.

  • @hsoley
    @hsoley 2 роки тому

    Best video on the topic

  • @ComSci-student
    @ComSci-student Місяць тому

    Great content Brian. Thank you.

  • @commandprompt7171
    @commandprompt7171 Рік тому

    thank you for explaining the erase method, I was confused from another video on how the element gets deleted by just assigning the last node

  • @chandragirivishnuvardhan7654
    @chandragirivishnuvardhan7654 4 роки тому +1

    @Brian Faure, Your explanation is pretty awesome. But, I think you have missed the "insert" operation.

  • @rensogomez5753
    @rensogomez5753 2 роки тому

    antipop to the mic and this is gold. thanks a lot.

  • @vipinamar8323
    @vipinamar8323 3 роки тому +5

    The length function is wrong, you need to add 1 to account for the last node.

    • @clashgamers4072
      @clashgamers4072 3 роки тому

      The head node(empty node at the start) will count towards the total so it kinda cancels out

  • @reynaldoruizflores
    @reynaldoruizflores 3 роки тому

    Thgank for the tutial as suggestion for future video. How to choose a Data structure? Pros vs Cons. Speed Big O notation

  • @akashp4863
    @akashp4863 3 роки тому

    Thank you. i know i can watch the video again and understand it

  • @motheotreasurepuso0724
    @motheotreasurepuso0724 8 місяців тому

    Great video. On your erase, I think it is essential to reassign the self.head for when erase is called at index= 0 so that it does not stay as None

  • @dontworryaboutit17
    @dontworryaboutit17 5 років тому

    This makes sense. My prof should not be teaching, haha. Thanks for explaining while making it so concise.

  • @GoldPlatedINDIAN
    @GoldPlatedINDIAN 6 років тому +6

    It would be simpler if when you append a new node, you simply point it to the head node and set the head as the node you just added. Saves you from iterating through all the nodes you've added (could be a large number) to add one to the end.

    • @dannyskillet7315
      @dannyskillet7315 9 місяців тому

      Pretty silly statement. I think in general terms when you "append" to a list, you are specifically adding to the end of the list not to the beginning.

  • @user-vr7pm5rx2f
    @user-vr7pm5rx2f Рік тому

    @Brian Faure. Thanks for your tutorial.

  • @gate7689
    @gate7689 4 роки тому +1

    I have a doubt. In the "append" method, new_node = node(data), each time The append method is called, an object of node class is created with the same name (new_node). Why doesn't this produces an error? wouldn't the object be garbage-collected?

    • @BrianFaure1
      @BrianFaure1  4 роки тому +1

      Hi, this isn't an issue because the Python garbage collector uses reference counting to decide what to remove from memory and since we are setting the 'next' node pointer of the prior node equal to this node there will always exist some reference to it (until it is deleted from the list).

    • @gate7689
      @gate7689 4 роки тому

      @@BrianFaure1 Thank you. That clears my doubt.

  • @ch33ze0g
    @ch33ze0g 3 роки тому +1

    Really dumb question, but in the display method why is cur = cur.next before elems.append? In my head you would want to append to elems first and then move the pointer

  • @lamedev1342
    @lamedev1342 3 роки тому

    Very useful, I'm in gr 11 and learning this it was well explained!

  • @PrinceAdom
    @PrinceAdom 8 місяців тому

    Instead of doing another costly iteration through the linked list to find the size of it. We can add a self.length variable to the init() with an initial size of 0, and then add a 1 in the append() and a subtract a 1 in the remove method

  • @b.f.skinner4383
    @b.f.skinner4383 3 роки тому

    Fantastic explanation of the topic, thank you!

  • @WillSmith-ui1pb
    @WillSmith-ui1pb 4 роки тому

    Very clear and concise. Thank you.

  • @maanupurisai398
    @maanupurisai398 4 роки тому +1

    I have a doubt.. If there is only one element say 5... Self. Data is 5 and self.next= none right?.. When you run "length" loop..the cur.next is equal to none, so it comes out and gives the length as 0...but the length should be 1 right?

  • @turk-money
    @turk-money Рік тому

    Great video, thanks for the post.

  • @dennisearle
    @dennisearle 9 місяців тому

    Thanks, Brian. Really helpful.

  • @akboss4341
    @akboss4341 4 роки тому

    Man !This is so crisp ! Nice tutorial

  • @jingyuchang1885
    @jingyuchang1885 6 років тому

    Thank you very much Brian! You videos are really amazing and helped me a lot! i really appreciate for all your effort! Just can't thank you enough!

    • @BrianFaure1
      @BrianFaure1  6 років тому +1

      No problem, thanks for watching Jingyu!

  • @JohnDoe-rr8uf
    @JohnDoe-rr8uf Рік тому

    Oh, it's like a snake game. Coded that yesterday.

  • @indranildutta4252
    @indranildutta4252 2 роки тому

    Watching in 2021... Very Cool....

  • @mizzzile
    @mizzzile Рік тому

    Quick and short recap.

  • @surajkumarkabbur2550
    @surajkumarkabbur2550 5 років тому

    Awesome video..👌👌
    One of the best I've seen for python tutorials.👌

  • @kelliroache2041
    @kelliroache2041 6 років тому +1

    Awesome tutorial! Thank you

  • @urass1765
    @urass1765 3 роки тому

    This video is very helpful for me to understand the linked list. I thought it would be very complicated 🤔

  • @rhnirsilva652
    @rhnirsilva652 8 днів тому

    bro posted the biggest DSA bang and left

  • @RishiKumar-zs4vj
    @RishiKumar-zs4vj 3 роки тому

    Really Useful keep on doing more videos please

  • @RahulKumar-tu7fm
    @RahulKumar-tu7fm 6 років тому +1

    @Brian Faure You did great tutorial. I really enjoyed the tutorial.

  • @jonassteinberg3779
    @jonassteinberg3779 2 роки тому

    You use function naming convention for your classes which would become very confusing eventually especially for new people. For example instantiating a class would look identical to assigning a function to a variable.

  • @nuandatun
    @nuandatun 4 роки тому

    Thank you so much! Your explanation really helped!

  • @sarvesh_k
    @sarvesh_k 4 роки тому +1

    hi @brian,
    getting error on if check
    if index >= self.length():
    TypeError: '>=' not supported between instances of 'int' and 'NoneType'

    • @BessedDrest
      @BessedDrest 4 роки тому

      I had the same error. It's possible that in your length() function you are printing the total, instead of returning it. self.length() needs to RETURN a value, not PRINT

  • @unclesam997
    @unclesam997 6 років тому +1

    Super good explanation!

  • @denysivanov3364
    @denysivanov3364 2 роки тому

    Great video! Thanks for your work!

  • @shrikantkarki4966
    @shrikantkarki4966 6 років тому

    Thank you sir.It helped a lot.Keep posting videos

  • @nestoriyagamongerto7881
    @nestoriyagamongerto7881 3 роки тому +1

    Awesome!
    But in method for calculate length I see some sort of problem.
    If counting start from 0 - head will not count. I understand, that list index start from 0, but in length method 0-element must counting like another 1. Or this is mistake?

  • @nostestwu8689
    @nostestwu8689 5 років тому +1

    for the len function, i think u should use curr instead of curr.next for the loop. Your code will give len -1 for the result

    • @bakerct90
      @bakerct90 5 років тому

      I think it works. In the len function it is technically counting the head which doesn't have data and not counting the last node. I believe it works as he intended though.

  • @umerehsan6677
    @umerehsan6677 Рік тому

    Great work. Really appreciated

  • @polsiv
    @polsiv 11 місяців тому

    Great video dude, it helped me a lot!

  • @ad2894
    @ad2894 6 років тому +1

    For the length method, should total + 1 not be returned since the last node won't be added to the total (because it doesn't satisfy the condition of the while loop)?

  • @hemanthkumarar
    @hemanthkumarar 4 місяці тому

    At 11:50
    I think it should be
    if index > self.length()
    instead of index >= self.length()
    This will ignore the last element and it cannot be accessed.
    I am still a newbie, correct me if I am wrong.
    Anyway, great series, thank you sir

  • @gabinkundwa7215
    @gabinkundwa7215 3 роки тому

    thank you for this tutorial it is simple and helpful

  • @hughg.rection6778
    @hughg.rection6778 4 роки тому

    Simple and clear, great tutorial!!

  • @YorkshireSpud
    @YorkshireSpud Рік тому

    Thanks for this video, you've explained the code and data structure really thoroughly and I'm starting to understand the implementation of LinkedLists.
    However, I found a bug with the length method that you wrote in the video. I found that if you try to access the last element of LinkedList it will raise the error due to the count starting at 0 rather than 1. The fix is simply start the count at 1 because it counts the empty node and makes the final element inaccessible via the get method that you defined.
    Also, with the change above, you want to move the `if cur_index == index: return cur_node.data` statement to the top of the while loop or you'll get an error stating cur_node.data is None (the get method)

  • @yulyalim5178
    @yulyalim5178 7 років тому +1

    Thanks for the video. Yes, that would be handy to have the code :)

    • @BrianFaure1
      @BrianFaure1  7 років тому +3

      Sure thing, I've added it to this git repository: github.com/bfaure/Python_Data_Structures/blob/master/Linked_List/main.py

    • @yulyalim5178
      @yulyalim5178 7 років тому

      Brian Faure Thanks! 🖖🏼

  • @BortSlampson
    @BortSlampson 6 років тому +1

    This is awesome, thank you!

  • @glenfernandes253
    @glenfernandes253 3 роки тому

    Fantastic explanation! Thanks!

  • @jimmorrisshen
    @jimmorrisshen 6 років тому

    Nice video. Good teacher. Great programmer.

  • @anjalipc8649
    @anjalipc8649 3 роки тому

    Thank you sir..It was very informative.

  • @sahajkapoor4718
    @sahajkapoor4718 5 років тому

    Pretty good tutorial for me as a beginner

  • @vivekkuma1
    @vivekkuma1 5 років тому

    Excellent Explanation Sir

  • @ROC4Life96
    @ROC4Life96 6 років тому +1

    What colour scheme did you use for your command line? I'd like to switch my terminal to that colour scheme

    • @BrianFaure1
      @BrianFaure1  6 років тому

      It's the default Ubuntu terminal color scheme with some extra background opacity ( askubuntu.com/questions/941734/whats-the-rgb-values-for-ubuntus-default-terminal-unity ). If you're on Windows 10 you can actually install the Ubuntu terminal directly and take advantage of all it's sweet features (and also the color scheme), there's some info on that here docs.microsoft.com/en-us/windows/wsl/install-win10 . Otherwise if you're on a lower version of Windows you can install Cygwin and customize the color scheme to look like this in the appearance settings. Similarly if you're on OSX or another Linux distro you'll have to tweak the appearance settings to match.

  • @limtis
    @limtis 3 роки тому

    Very helpful, thank you!

  • @vaibhavtiwari2941
    @vaibhavtiwari2941 5 років тому

    Thanks for your wonderful tutorial.
    I was stuck in the linked list part from about a week .
    You were awesome

  • @GuitarreroDaniel
    @GuitarreroDaniel 3 роки тому +1

    This gets the idea of a linked list. However, it's not a very efficient implementation. With that append method you'd be iterating the WHOLE linked list before each insertion, and avoiding that kind of insertion is the very reason of using a Linked List. This could be easily fixed by keeping track of the Linked List's tale node.

  • @zusm
    @zusm 3 роки тому

    for get you can just have display return elems instead of print that way you can just return self.display()[index]

  • @precisionchoker
    @precisionchoker 5 років тому

    Many thanks , I've learnt a lot :)

  • @SLowe-xi3fq
    @SLowe-xi3fq 4 місяці тому

    Can't we just say while current != None (or while current)? we dont necessarily need to make sure we have 2 nodes before null right?

  • @nr7343
    @nr7343 Рік тому

    Lisr length to.. just keep track when add new node