Data Structures: Linked Lists

Поделиться
HTML-код
  • Опубликовано: 26 сен 2016
  • Learn the basics of Linked Lists. This video is a part of HackerRank's Cracking The Coding Interview Tutorial with Gayle Laakmann McDowell. www.hackerrank.com/domains/tut...
  • НаукаНаука

Комментарии • 357

  • @wolfferoni
    @wolfferoni 6 лет назад +563

    You know that moment of satisfaction when you finally understand something? I got it from this video. I've spend so much time watching my lectures, looking at tutorials and explanations of LinkedLists and never really understood it. I decided to write you the code and try to explain to myself what each line meant and when I got stuck, listened to you explain it with the little diagrams and finally(!) understand what on earth this node and head and next thing means. THANK YOU.

    • @patcoder1287
      @patcoder1287 4 года назад +3

      @Ella Blun spot on.
      It's called removal of bias
      Very effective tool.
      Sad how we humans are literally built to doubt every damn thing unless reinforced by multiple sources
      But it's the reality we must live with.

    • @jasoncarzano9388
      @jasoncarzano9388 3 года назад

      Yeah

    • @alfonsocanady7564
      @alfonsocanady7564 2 года назад +4

      It’s been 4 years, what are you doing now?

    • @Vrinda219
      @Vrinda219 Год назад

      Nice

    • @reynaldosilva1115
      @reynaldosilva1115 Год назад

      @@alfonsocanady7564 he is still trying to solve linked list problems,… haha ask me what I’m doing in 4 years!!

  • @grace1081
    @grace1081 6 лет назад +48

    Gayle, if you ever see this, I really love the way this mini lesson is organized and delivered. Very helpful I thank you I am passing a class because of you!!!

  • @MannyGonzalezReyna
    @MannyGonzalezReyna 6 лет назад +27

    Thanks so much for putting the algorithm and the coding together on the same page, that's the best way to learn any coding language.

  • @eliastefera8574
    @eliastefera8574 Год назад +3

    This is probably the best video you can find on RUclips that talks about LinkedList. Great job!

  • @elbuhdai605
    @elbuhdai605 4 года назад +22

    Thank you so much. The visual representation that changes with each new line of code is so helpful for a concept like this. Code for nodes can often look kind of cryptic without a visual representation of how things move.

  • @gnanasekaranebinezar7199
    @gnanasekaranebinezar7199 2 года назад

    I have been looking for crystal clear explanation to algorithms such a long time. Finally I landed up on right place. What a diligent explanation. Learned a lot. Splendid. No words to appreciate further.

  • @Buebrowndj
    @Buebrowndj 2 года назад +2

    Excellent, easy to digest explanations and 0 ads. Thank you for your content

  • @user-xr2ik2ci2t
    @user-xr2ik2ci2t 4 года назад +5

    Great video! I was so confused with Linked Lists in my CS class, but this makes it so simple to understand!

  • @kidou123456
    @kidou123456 4 года назад +1

    The wrapper class of LinkedList dynamically changes the head by prepending to inform other "lists" pointers. This is really neat! Thank you!

  • @markkfitx
    @markkfitx 6 лет назад

    This is one of the few videos that I found does not make the topic more confusing. Thanks so much

  • @nitishmehta9322
    @nitishmehta9322 5 лет назад +10

    Really amazing tutorial on Linked lists, thank you!! Just want to add why accessing array is faster because its elements are always stored in contiguous memory locations this is kind of its disadvantage also as you need to have in advance that contiguous chunk of memory in advance. Feel free to correct me if I am wrong.

  • @BrianFaure1
    @BrianFaure1 6 лет назад

    For anyone interested in a simple Python implementation, feel free to peep my recent lesson: ruclips.net/video/JlMyYuY1aXU/видео.html

  • @khatharrmalkavian3306
    @khatharrmalkavian3306 4 года назад +162

    Any conversation about linked lists should BEGIN with the following warning:
    "You probably shouldn't be using a linked list for this."

    • @pvic6959
      @pvic6959 3 года назад +4

      @Peterolen i have no idea why that person said that. linked lists absolutely have their place. this is like saying "You probably shouldn't be using a _hash table_ for this." Well, if you just need to store a list of things, then yes, you dont need a hashtable. But if you need a quick mapping, 100% use a hashable. All data structures have their uses and strong points

    • @pvic6959
      @pvic6959 3 года назад

      @Peterolen But an ArrayList is just an implementation of a linked list. I don't really see it as different. However, if we treat them as different, I agree with you. I can only remember using LinkedList (the data type) like once lol. Most of the time i used ArrayList. But to me both of these are linked lists (the concept, not the data type)

  • @scottrichman798
    @scottrichman798 6 лет назад

    Wonderful explanation. Textbook for my class gets confusing sometimes since its not step by step so these videos help explain further

  • @francis-comedy-club
    @francis-comedy-club 3 года назад

    of all the "LinkedList" videos I've watched, None can be compared to this. Thank you for this explanatory video 🥳🥳🥳🥳🥳🥳🥳🥳🥳

  • @rajatsharma3673
    @rajatsharma3673 4 года назад

    your explanations are so smooth I'm so thankful to you ma'am.

  • @abcstephanie
    @abcstephanie 5 лет назад +1

    thank you!! you explained this so beautifully clear. kudos

  • @nkeiruw5873
    @nkeiruw5873 3 года назад +8

    I've never watched a programming video from beginning to end before. A truly class explanation. Thank you!

    • @jehrilipaad5075
      @jehrilipaad5075 3 года назад +1

      SIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIMPHHHHH

    • @xfire3778
      @xfire3778 3 года назад

      Jehrili Paad couldn’t agree more

  • @ej-14-
    @ej-14- Год назад

    I re-watched this video several times, thank you for posting this!

  • @mac_88_59
    @mac_88_59 6 лет назад +1

    understood this way better the way you explained it compared to how my professor did. thank you so much.

  • @saindst
    @saindst 9 месяцев назад

    The one thing I have come to find out about all these data structure explainations on youtube is that only a hand few will ever give you an actual REAL life damn example of where and HOW it is used so you can actually make sense of it all. It makes a HUGE, a HUUUUUUUGE difference.

  • @sarthakgupta2528
    @sarthakgupta2528 6 лет назад

    great explanation!! right from the scratch. this helped me a lot, thanks!!

  • @looseyfur6669
    @looseyfur6669 5 лет назад +6

    omg someone i can understand!! amazing, thank you for speaking clearly! that is hard to come by in this area!!

  • @cast1439
    @cast1439 3 года назад

    Thanks for the lesson! It really helped me understand the code much better. Also, I do believe you have a problem with your append method. Your linked list will end up adding the first element twice.

  • @user-px9zz3fo9o
    @user-px9zz3fo9o 8 месяцев назад

    You know that moment of satisfaction when you finally understand something? I got it from this video.

  • @thanhattran284
    @thanhattran284 3 года назад

    Great vid! I understand it faster than 100x times thanks to you

  • @segintendorocks
    @segintendorocks 5 лет назад +22

    This girl is amazing. Thanks for the fantastic tutorial :D.

  • @guptayash4
    @guptayash4 5 лет назад +4

    The deleteWithValue(int data) function will throw NullPointerException when the element does not exist in the LinkedList.
    For Example, LinkedList: 7 (List has only one Node, I.e. only head node with value 7)
    And, you try to delete the node with value 5.

  • @cindy__4920
    @cindy__4920 6 лет назад +4

    This video is very helpful. Thank you!

  • @Fivousix
    @Fivousix 7 лет назад +75

    Great videos, I have been watching some and I really like the way you draw at the same time you show the code. They are short and straight to the point.
    I would just like to point out for future viewers that in C/C++ if you do not explicitly delete those nodes memory will be leaking.

    • @pascal3889
      @pascal3889 7 лет назад +3

      Yes. While using C/C++, after deleting a node you also need to free the memory which was allocated to the node. The programmer is required to handle memory management in C/C++.

    • @damnstupidoldidiot8776
      @damnstupidoldidiot8776 5 лет назад

      delete head;
      head=head->next;

    • @damnstupidoldidiot8776
      @damnstupidoldidiot8776 4 года назад

      @@xCwieCHRISx Whoops, my bad!
      head=head->next;
      delete head->previous;

    • @TheKhakPakflyest
      @TheKhakPakflyest 4 года назад +1

      Dope! I was watching this from a C++ background and was thinking to myself, "java doesn't have to explicitly free memory?" xD

    • @ronaldskorobogat3152
      @ronaldskorobogat3152 4 года назад +3

      @@TheKhakPakflyest Java has the garbage collector and any time it sees that an object can't be reached, like in the video where we skip over that Node object, it automatically deletes the object and frees up the space. Something C++ doesn't offer ;)

  • @wibowester
    @wibowester 5 лет назад +1

    Reallly thank you for explaining!!! I learned a lot from it!

  • @tannerbarcelos6880
    @tannerbarcelos6880 5 лет назад +1

    Linked lists are one data structure I really understand with not much of a problem. Although pointers are still a struggle for me, the linked list makes a lot of sense.. I guess practice, practice, practice will help!

  • @YouTube_Staff
    @YouTube_Staff 4 года назад

    snaps out for you, this is such a great explanation.

  • @alcatraz8623
    @alcatraz8623 2 года назад

    a video from 2016 gave me the info i needed, thank you

  • @owonubijobsunday4764
    @owonubijobsunday4764 5 лет назад +1

    Thank you very much.
    You just lifted a load off me head

  • @aycanocal6506
    @aycanocal6506 4 года назад

    Great tutorial, easy to understand! Thank you!

  • @abrahamparker6140
    @abrahamparker6140 5 лет назад

    Excellent emphasizing. Thanks a lot.

  • @rsmease
    @rsmease 6 лет назад

  • @moneytree7924
    @moneytree7924 5 лет назад +9

    Appending a node to a linked list doesn't have to be O(n) if we keep track of the `tail` node as well as the `head` node. But very well done explanation as usual, Gayle! Your content are always such confidence boosters!!

    • @saveerjain6833
      @saveerjain6833 2 года назад +1

      is that not just doubly linked?

    • @sanjeen2503
      @sanjeen2503 Год назад

      ​@@saveerjain6833 no.

    • @murloc_rampage3856
      @murloc_rampage3856 7 месяцев назад

      @@saveerjain6833 It's not doubly because doubly linked lists have a pointer to the previous and next nodes. To Append at O(1) all we'd need is for the linked list to have both a head var and a tail var. By keeping track of a tail node, we make appending at the end O(1) because we have instant access to the tail node as well as allowing us to assign the new tail node and set it to keep track effectively. Then by making it into a doubly linked list this makes searching faster do to our doubly linked list allowing us to search both ends of the entire set to quicken searching. so having a tail node by the linked list definition does not make it a doubly linked list. It's the nodes themselves that have a pointer to before and after nodes that makes it doubly linked. I hope this makes sense.

  • @xuwang1292
    @xuwang1292 5 лет назад +6

    if you talk about the usage of this structures.Such as where can we use this structures and where can not. I think this video will be much better.

  • @omarm9794
    @omarm9794 5 лет назад

    thank you so much for the video. now i understand the linked lists.

  • @mk177
    @mk177 3 года назад +1

    This is otherworldly explanation technique, amazing. "Actionpacked supercoder blockcuster" ! :)

  • @anonymousvevo8697
    @anonymousvevo8697 4 года назад +1

    i just loooove you , and your explanations

  • @bakihanma1865
    @bakihanma1865 2 года назад

    Gayle is awesome, I got her book, only read 1st chapter so far .

  • @Dpaz2009
    @Dpaz2009 7 лет назад

    i love this channel :) it's so good!

  • @fzane14
    @fzane14 3 года назад

    Thank you so much, this was very easy to follow.

  • @KaisarAnvar
    @KaisarAnvar Год назад +1

    She is SUCH A GENIUS

  • @shaikmasthan8254
    @shaikmasthan8254 4 года назад

    That is an amazing explanation. I have two questions
    1. Does the deleted node garbage collected?
    2. How to delete a node with duplicate data. I.e.If there are multiple nodes with same data and we want to delete the last data Node. ? How do we differentiate the two nodes with same data ?

  • @tannerbarcelos6880
    @tannerbarcelos6880 5 лет назад

    Appending and displaying both recursively or just with a loop I practically have down. It’s the destructor and the delete() methods in c++ where the struggle kicks in a bit

  • @FBGZ
    @FBGZ 5 лет назад

    in link lists can you have different types of data at the same time? such as intergers and strings at a same time

  • @ardiansyah6794
    @ardiansyah6794 4 года назад

    I wished she was my lecturer.. so fluent and to the point 😀

  • @hassankazmi5443
    @hassankazmi5443 7 лет назад

    Awesome job Gayle!

  • @sundarb6673
    @sundarb6673 5 лет назад

    Thank you, Chris Gayle!

  • @maymotivation6215
    @maymotivation6215 3 года назад

    your voice is so sweet i grasped everything u said

  • @damilola_adegunwa
    @damilola_adegunwa 2 года назад +1

    thanks for this. I'm trying to find problems that would fit LinkedList best

  • @asivaprasadam
    @asivaprasadam 3 года назад +4

    Minor correction with the function:
    deleteWithValue (int data);
    Change: Move the special case if statement completely to the end of the while loop.
    Reasoning: If the head and the next node has the same value and equals the data value. The next element won't be deleted. So the special case should be after the while loop. Correct me if I'm wrong.
    Example data for the original function to fail : 10, 10, 20, 39, 48, ... data = 10

    • @chelseyfoster7260
      @chelseyfoster7260 2 года назад

      She could also change the if statement that checks if the head needs to be deleted to a while statement.

    • @sanjeen2503
      @sanjeen2503 Год назад

      she has mentioned the requirement of the function - delete only the first node having data same as the given data

  • @colinbecko
    @colinbecko 6 лет назад

    Very understandable, thank you! Subscribed!

  • @crackcodinginterview4995
    @crackcodinginterview4995 4 года назад

    That’s a nice way to explain Linked list!

  • @vakameli
    @vakameli 6 лет назад

    Thank you ! Very nice explanation !

  • @phillyeagles0728
    @phillyeagles0728 6 лет назад

    Thanks for the video. Just wondering, would it be fine to have a contructor in LinkedList, that would take a node as a parameter, and allow you to create LinkedList objects? Or maybe it's useless?

  • @idilsaglam1278
    @idilsaglam1278 3 года назад +1

    This video is very helpful! Thanks a lot !!!!

  • @blyatman635
    @blyatman635 5 лет назад

    Can you introduce another node in the Linked List class that will remember the last member we add, something like tail, and when adding you just add to the tail and make it O(1) instead of O(n) ?

  • @oscarwang7920
    @oscarwang7920 5 лет назад

    Thanks for the explanation !

  • @BruceKarrde
    @BruceKarrde Год назад +1

    So, do I understand correctly, that with this linkedlist we're only adding values and re-routing instead of actually removing values? If so, would this mean that the size will increase over time? If so, is there any way to access the values that have been 'ignored'?

  • @SamvitAgarwal
    @SamvitAgarwal 6 лет назад +4

    Hey, great video! I was just wondering, however, when is using a linked list actually useful? Since for most operations it requires linear time, versus an array which requires constant time for most operations, what is the main advantage of using a linked list versus an array?

    • @xXZombieHunter0802Xx
      @xXZombieHunter0802Xx 5 лет назад +9

      My main appeal is that it's dynamic and the memory isn't assigned when compiled, like an array. An array has to be whatever length you initialize it to, but you can continually add to Linked Lists. The reason is that arrays use memory locations right after each other, so the computer needs to know how far to go before stopping. The Linked List uses the a memory address, so as long as it knows where to go, it doesn't need consecutive memory locations, and be continuously added to without overflow.

  • @kakashi99908
    @kakashi99908 2 года назад

    Linked list are used on existing objects like Bags or even implemented in the bag itself. Is there a name for things like Linked List? I think they are called sorting algorithms..

  • @abdelrhman110
    @abdelrhman110 6 лет назад +277

    My God, that keyboard click sound is erotic. What keyboard is that?

    • @shadowalkzx
      @shadowalkzx 6 лет назад +18

      iMac keyboard

    • @masteryoda3679
      @masteryoda3679 6 лет назад +24

      Abd-Elrhman Rizk lmfao. I thought the same thing and wondered if i was the only one who enjoyed that sound until I went to the comments section and saw this. I'm glad i wasn't the only one. Lol.

    • @scwalcofscwalcof1404
      @scwalcofscwalcof1404 6 лет назад +25

      Get yourself a mechanical keyboard and the sound of this keyboard will make you cringe.

    • @megamanx2006
      @megamanx2006 6 лет назад +5

      Abd-Elrhman Rizk dude, if sounds like that are your thing, search ASRM audio or something like that...

    • @mirknankazmzad8517
      @mirknankazmzad8517 6 лет назад +11

      omg thanks, i searched and i'm listening to it like 1 hour or something. this is great. like braingasm

  • @vivekjadon2986
    @vivekjadon2986 7 лет назад

    One question. if I make use of a temporary object to store current node in append function, then I won't need the LinkedList class containing head separately. I can implement it using just one class. I will use that temporary object to reach the end of linked list while the head will remain the same. Is this correct?

  • @r1ch007cs123
    @r1ch007cs123 2 года назад

    Great explanation of the linked list.

  • @plttji2615
    @plttji2615 2 года назад

    Thank you for the video, I am preparing for the following interview question. When searching for the element in the list, what if the list is too big, then what would you do? I am really struggling how to answer this question. Could you please help me?

  • @abhishekbedi1432
    @abhishekbedi1432 6 лет назад

    Awesome Video! Can you please include insert at position also.

  • @paladin1147
    @paladin1147 4 года назад

    Put the return at the end so if you there are any duplicates in the (DeleteWithValue) it wont skip them over.

  • @simiiv5021
    @simiiv5021 4 месяца назад

    You made it so easy!!!

  • @brontiago
    @brontiago 2 года назад

    If you keep a tail variable along with the head, appending to the list should be 0(1) since you won't have to traverse the whole list to the end

  • @miguelvalente2572
    @miguelvalente2572 7 лет назад

    Simple question about creating the LinkedListClass.
    In oracle when creating a class its needs to have the same name as the CLASS FILE name. So how to circunvent this problem.

  • @akshaygupta256
    @akshaygupta256 6 лет назад

    Nice video, But I am confused about the dot operator . Since current and next are node type variable, how can we use current.next? because all I know is only object.( method/data) can be used.

  • @johnnyniklasson2956
    @johnnyniklasson2956 2 года назад

    Loved it! thanks

  • @lohithn8557
    @lohithn8557 4 года назад

    On deleting of data, do head element will be updated? As we are assigning head to current node variable and delete operation is performed on it.

  • @manivannan39
    @manivannan39 5 лет назад

    while appending an element does the head node keep changing and we add a new node after the head node? , is that the meaning of the append code in the video

  • @CarlitoProductions
    @CarlitoProductions 6 лет назад +3

    This is super clear to me, and I'm briefly flipping through data structures since someone told me I should learn them to apply for work. I understand how it's set up in Java, but how would these be implemented in Javascript outside of DOM?

  • @malakdesouky9123
    @malakdesouky9123 3 года назад

    Which coding platform did you use? Thank you this was very helpful!

  • @suryadevarakarthik1814
    @suryadevarakarthik1814 3 года назад

    Amazing explanation 👨‍🔬

  • @hashfromjc
    @hashfromjc 6 лет назад

    I dont understand deleteWithValue(). Dont you just create current as a temporary list that copies the data of head, and then works through the data until value is found. So I understand that you manipulate the pointers in current to ignore the node you need to, but dont you need to change in head (the original link list) to match what current now has? How do you do so? That block of code is left at current = current.next

  • @captainutki01
    @captainutki01 5 лет назад +1

    We can't have two public classes in the same file though right?

  • @mergatafa8501
    @mergatafa8501 7 лет назад

    The best lecture I have ever listened

    • @mjohnson510
      @mjohnson510 7 лет назад

      seriously! This is the best one I've seen. She's hella good

  • @veronika_franz
    @veronika_franz 6 лет назад +1

    It's a very good video.
    The method deleteWithValue ...
    when you have a LinkedList with
    headvalue0 | linkedToNextValue1 -- value1 | linkedToNextValue2 -- value2 | linkedToNextValue3 -- value3 | linkedToNull
    and you want to delete value2,
    you go to value1 and set the former "linkedToNext2" variable to "linkedToNextValue3", so basically you work around as said in the video.
    But this does not delete Value2, right? Because value2 is still linkedToNextValue3, so it should not be collected, should it?
    headvalue0 | linkedToNextValue1 -- value1 | linkedToNextValue3 -- value3 | linkedToNull
    value2 | linkedToNextValue3 --^
    So, basically you have information hanging around, haven't you?
    To really delete value2, wouldn't it be necessary to set the link of "value2 | linkedToNextValue3 --^" to "value2 | linkedToNull", so that this element can be collected, like this:
    headvalue0 | linkedToNextValue1 -- value1 | linkedToNextValue3 -- value3 | linkedToNull
    value2 | linkedToNull
    so, this would be:
    ...
    while(current.next != null) {
    //if data of the next element of the current element
    //equals the data we want to delete
    if(current.next.date == data) {
    //set the next-variable of the current element
    //to the value safed in the next-variable of next element (so it's next next)
    current.next = current.next.next;
    //and set the next-variable of the next-element to null
    //current.next.next = null;
    }
    }
    NullPointerException should in this case not occur, as next-element exists (because we found it and want to delete it), so there is a variable that can be set to a certain value, in this case it's null. And we have an exception for the last element. And only the last element can contain null in the next-variable.
    And considering that LL are used for many elements, because that's why they are so practical (better than arrays with specific length), plus the operations that can be used over a longer period of time, this looks to me like the outcome is something like a christmas tree/one data string with information hanging around.
    Is there somewhere an explaination how java's LinkedList is structured/built? I think you cannot actually get the actual Node (Node current = ...), but have to work through with methods such as element(), ... So, I am actually looking for something that can be used for a Node variable (as I would do it in the LL that I built on my own).

  • @joaocampos9615
    @joaocampos9615 3 года назад

    Hey, @HackerRank. There was only one thing i did not understand : "If we preappend we have a issue because we change the Head..." Only with preappend ? i guess we always need a Wrapper/class and a "stamp" on one node to navigate and do operations and always know which one is the first. To not miss a single node. Is this correct ?
    Anyway, thanks for the free learning :)

  • @osmanmusse9432
    @osmanmusse9432 2 года назад

    Great Explanation

  • @yousefbaghlaf6713
    @yousefbaghlaf6713 5 лет назад

    What if we wanted to show the elements of the linked list? Like a show method?

  • @koshykoshy9276
    @koshykoshy9276 6 лет назад +1

    Thank you

  • @elcat9091
    @elcat9091 5 лет назад

    At 5:31, it is stated that current is a method. Isn't it an instance variable of type Node?

  • @felipefallas2726
    @felipefallas2726 3 года назад

    but what if I want to delete what's inside a node, taking as a parameter a reference to the data inside said value, and not the number of node itself, how would you approach that?

  • @alexalmanza8989
    @alexalmanza8989 5 лет назад

    is there a better way to remove the head? since you aren't able to access it wouldn't that cause a memory leak? or is it different in java

  • @j3froc63
    @j3froc63 4 года назад

    So linked list are actually objects that point to another object? And at 2:30 when you write 'Node next' does the program already know what 'next' does? Is 'Node next' like a method that is predefined within the program??

  • @quantalk_lecturequantalk3409
    @quantalk_lecturequantalk3409 5 лет назад

    Question! Why the data's type is int, we can store any data type in node right? so why (int data)

  • @chodfanclub8004
    @chodfanclub8004 5 лет назад

    Won't the delete function only delete the first instance of a node with matching data? Not every instance?

  • @shinigami0018
    @shinigami0018 6 лет назад

    Hi guys, Please which tool are they using to create explanation visuals? I want to create some for a class I'm teaching

  • @PholoshoSeloane
    @PholoshoSeloane 3 года назад

    you are theeeeeee best.. I love this

  • @Tony.Nguyen137
    @Tony.Nguyen137 3 года назад

    The class node has only 2 fields, Node next and int data. What if I want to add a String to my LinkedList, to which field is the string allocated??

  • @TimScratch
    @TimScratch 5 лет назад

    Would nesting the class node under linked list make a difference?