Singly Linked List | Insert, Delete, Complexity Analysis

Поділитися
Вставка
  • Опубліковано 31 січ 2025

КОМЕНТАРІ • 135

  • @BlueTreeCode
    @BlueTreeCode  3 роки тому +8

    Important Note: For deleteAfter and deleteStart, if these methods had a return type of void it would be fine, however, since these methods return a reference, you should update toDelete.next to null before returning toDelete. This is because toDelete holds a reference to the entire list and we only want a reference to the single node that's being deleted.

  • @marhawk6468
    @marhawk6468 3 роки тому +17

    I’m going to recommend anyone confused about linkedlists to this video! Hands down the best one on UA-cam. Thanks!!!

  • @rahulkhandelwal7034
    @rahulkhandelwal7034 4 роки тому +9

    Wow that's amazing in just 15 min I have learnt 6 types of insertion and deletion at glance .
    Thanks for this nice content.

  • @RajeshSamson
    @RajeshSamson 4 роки тому +5

    Your video is truly the best one in entire UA-cam to learn data structures in java

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

    Thank you for the comprehensive explanations. This has helped me prepare for the exam in my OOP2 class :)
    Edit: Please make more videos that give a thorough treatment to intermediate and advanced topics in programming. I found that videos on the topic of linked lists were quite dumbed down and didn't have the depth that I needed to flesh out my understanding. until I found your channel. The "beginner's guide" collection of programming videos seems to be quite saturated, but those are of limited utility. We need more videos like yours on youtube.

    • @BlueTreeCode
      @BlueTreeCode  10 місяців тому

      The key was to really help ace the Data Structures course. Please don't forget to share / recommend the channel to others / on social media to help it grow.

  • @KeenalShah
    @KeenalShah 4 роки тому +11

    This cleared the whole concept up for me. Thanks for providing with simple explanations and great visuals, subscribed!

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

      Thanks, Keenal Shah! Glad it helped :)

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

    OMG!!! Man that visual representation made everything crystal clear love u man.

    • @BlueTreeCode
      @BlueTreeCode  10 місяців тому +1

      Thank you so much!!! Please don't forget to share the channel with others as it will help it to grow.

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

    Best video ever! Been struggling with linked list for a while and this just brought the pint home!

    • @BlueTreeCode
      @BlueTreeCode  10 місяців тому

      Glad you found it helpful!! Please don't forget to share / recommend the channel on social media to help it grow.

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

    You are the best. You left no stone unturned.
    Thank you👍👍

    • @BlueTreeCode
      @BlueTreeCode  10 місяців тому

      Thank you so much! Please don't forget to like and share as it will help the channel grow.

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

    That's the best video about Linked lists, EXTREMELY HELPFUL
    Thank you so much BTC

    • @BlueTreeCode
      @BlueTreeCode  10 місяців тому

      Thank you so much! I'm glad you found it helpful! Don't forget to share the channel with others.

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

    These are some amazing and concise videos that really help visualize these concepts and algorithms. Thank you so much! Subscribed!

    • @BlueTreeCode
      @BlueTreeCode  10 місяців тому

      Thank you so much!! Please don't forget to share the videos as it helps the channel grow.

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

    you are the legend of the explanation

    • @BlueTreeCode
      @BlueTreeCode  10 місяців тому

      Thank you so much! Please don't forget to like and share as it will help the channel to grow.

  • @Super_Shaq
    @Super_Shaq 13 днів тому

    Incredibly helpful video - thank you!!

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

    Best one on youtube i hope u continue

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

    Is it just me or this channel videos does not come out when i search it, like im looking at his video about doubly linked list and i scroll so far down, still cant see it. Its so annoying cus this video is so much better than those thats being recommended.

    • @BlueTreeCode
      @BlueTreeCode  10 місяців тому

      Thanks a lot! Not sure why that's happening, but what you can do is like and share the content if you found it useful.

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

    The best explanation of linked list I have ever seen.

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

    This was a really good explanation with good diagrams! Helped me understand the concept completely! thanksss

    • @BlueTreeCode
      @BlueTreeCode  10 місяців тому

      Thank you so much!! Please don't forget to like and share the video on social media as it will help the channel grow.

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

    This is PHENOMENALLY helpful, thank you so much!!!
    😭😭😭

  • @mrunalinidudhagawali1945
    @mrunalinidudhagawali1945 5 років тому +2

    Thanks for clearing time complexity for different cases (front end and after)

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

      Glad it's helped, Mrunalini Dudhagawali.

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

    Thank you soooooooooooooooooooooooooooooooo much after months of banging my head just by animation you made me understand this. Unbelievable. Thank you again

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

      I'm very happy that it's helped you immensely, Muhammad Ali. :)

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

    Explained very well, thank you so much!

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

    YOU ARE A LIFE&TIME SAVER!

    • @BlueTreeCode
      @BlueTreeCode  10 місяців тому

      Thanks a lot!! Don't forget to like and share the channel on social media as it will help it grow.

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

    It was very precise and helpful. Thank you. Keep up the good work.

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

    Hats off , excellent animations and explanation.

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

    Amazing explanation. I'm now confident in this. Thanks

    • @BlueTreeCode
      @BlueTreeCode  10 місяців тому

      Glad you're confident! Please don't forget to share / recommend the channel on social media to help it grow.

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

    you just saved me from 1 week of 3 hour sleep
    Thank you

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

    Thanks a lot Blue Tree Code , you make very clear for a me , because i am at learning stage , thanks a lot

    • @BlueTreeCode
      @BlueTreeCode  10 місяців тому

      I'm glad you've learnt a lot. Please don't forget to like and share the videos/channel to help it grow.

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

    Excellent explanation , Thank you

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

    Bro you saved my bacon! Thank you so much for the tutorial!

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

      Thanks for the kind comment, Night Stalker! :D

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

    Really very very clear and helping me much get those concepts

  • @sumanthpai5579
    @sumanthpai5579 5 років тому +2

    subscribed , I dont wanna miss your videos :) , please keep up with more videos , cheers mate :)

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

    You are a god bro you Help in a big problem thank you bro😊

    • @BlueTreeCode
      @BlueTreeCode  10 місяців тому

      Thank you so much!! Please don't forget to share / recommend the channel on social media to help it grow.

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

    Very nice presentation

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

    This was great, thank you so much!

    • @BlueTreeCode
      @BlueTreeCode  10 місяців тому

      Thank you!! Please don't forget to like and share as it will help the channel grow.

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

    9:53 Why does returning curr mean the node curr is referencing gets deleted from the chain?

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

      Hi BoostRoo. When I said "and that's how I would delete the node at the end of the list", I meant the method in general, not by the statement "return curr". Returning curr just gives us access to the node after removing it from the linked list. The deletion of the last node comes from removing the pointer that is holding that node in the list (8.next). Hope this helps :)

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

      @@BlueTreeCode Hey, thanks for the reply! Similarly at 10:41, how is the node that toDelete is referencing deleted from the list when toDelete.next is still pointing to the node that head is referencing? Isn't it therefore still attached to the overall list and thus not deleted?

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

      ​@@BoostRoo You're welcome BoostRoo. Yes, good catch! toDelete.next does have a reference to the head of the list. Although it's unreachable from the new head, it's still part of the list, so toDelete.next should be set to null to ensure manipulating that node will not mess up the actual list.
      I will add this edit to the description :)

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

      @@BlueTreeCode Ok great - this was confusing me a lot! And so the toDelete node is not deleted from memory right? What if we set toDelete.next to null? Would Java delete it from memory through automatic garbage collection, or somehting? Or would it just remain in memory, essentially in limbo as it's now inaccessible?

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

      @@BoostRoo Thank you for bringing it up! Once the referenced node being returned is assigned to another reference variable, it will not get garbage collected since another reference variable is now referencing it. The following gives you the option to keep the deleted node.
      Eg) Node deletedNode = deleteStart();
      However, if we do not assign it to another reference variable, then it will get scheduled for garbage collection whether or not we set toDelete.next to null. This is because toDelete is a local variable (so it gets destroyed once the method finishes executing).
      Eg) deleteStart();

  • @IsraaMohamed-w8b
    @IsraaMohamed-w8b Рік тому

    that was very helpful! thank you

    • @BlueTreeCode
      @BlueTreeCode  10 місяців тому

      Glad you found it helpful! Don't forget to like and share as it will help out the channel.

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

    11:40 Is this method to delete a specified node, or to delete the node after a specified node? It seems like it's the latter, by would you do this instead of just simply specifying the node you want to delete? I'm curious why you didn't describe a method simply saying how to delete a specified node :P

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

      Hi BoostRoo, so basically these methods are exercises to help everyone understand how to manipulate linked lists. I'm not sure why I didn't add a method to delete a specific node, but you can check out my video on "Introduction to graphs". Over there I have a method to delete a node in a LinkedList. (NB: You don't need to understand graphs) at **** 21:13 ****

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

      @@BlueTreeCode Cool, thanks :)

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

    Great video keep going

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

    love ur videos !

    • @BlueTreeCode
      @BlueTreeCode  10 місяців тому

      Thanks! Don't forget to like and share.

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

    Great explanations thank you.

    • @BlueTreeCode
      @BlueTreeCode  10 місяців тому

      Thank you so much. Please don't forget to like and share the channel on social media to help it grow.

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

    This was really helpful 👏.thanks man😊

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

    Hey Everyone! Thanks for checking out my video! Don't forget to Like, Subscribe, and MOST IMPORTANTLY click that Notification Bell for updates! :)

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

    Thank you for clearing this up

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

    Superb explaination

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

    thanks for the tutorial. But, I have a doubt about the space complexity of the insertion node algorithm. It was not considered extra space because it was only one node you are adding. If instead of a node was a group of nodes like an array, then the space complexity was O(n). That's the concept of extra space? thank you for the explanation.

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

      @Luisa Pérez, you're welcome :) So, if we're thinking about space complexity, we would be considering space outside the given input array. The algorithms in this video use a constant amount of extra space, so it does use some negligible space, such as for creating variables, a node, etc, which translates to the O(1) in the video (I should have clarified that). Going back to your example, if you're given an input array with say 'n' nodes, and the memory outside your input array (used by your solution) is not directly related to size of your input array(e.g. you did not create an extra array that is 1/2 n in size), then you have a constant space algorithm. If ,however, in your question, you decide create an array with say 1/2 the size of n every time your algorithm runs, then you'd have a linear space algorithm on your hands, as 1/2 n translates to O(n) space complexity. Hope this helps :)

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

      @@BlueTreeCode thank you. I understand better now.

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

    this is awesome video but i still confusing if what is better in data structure hard coding or using a import ?

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

      Hi Johnpaul, for a university course you should be expected to build data structures from scratch. If you're applying for a tech job, you may be required to build these from scratch as well, so it's good to be knowledgeable on this.
      However, when writing software, It's extremely rare I would be building a data structure from scratch. I would simply import the specific DS since the Big O would be identical to a custom implementation.

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

      Please upload more videos its really help a lot

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

    how is inserting a node at the end a constant space complexity if you have to traverse the node to get there? 13:25

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

      Hi Stacey Loulouse, so I think you might be referring to time complexity. It takes linear time to traverse to the end of the list. However, space complexity refers to the amount of extra space used by the algorithm. Inserting at the end uses a constant/fixed amount of space (regardless of the length of the list, only one node and some set space for variables are being created). Hope this helps :)

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

    So I used this code to then create a linked list but how do I call these functions to addtoend??

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

    Hi
    At 7:21
    can you please explain to me
    1) n.next=curr.next;
    2) curr.next=n;
    and
    3) curr = curr.next;
    in detail .
    I'm stuck at these 3 statements ??

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

      Hi Thanos, sure!
      This is the linked list we're given.
      curr
      | 4 | ---> | 12 | ---> null
      This is the node we want to insert.
      n
      | 8 |
      Think of it this way:
      Nodes = Blocks Floating above the ocean.
      References between Nodes = Ropes with hooks on the end that keep blocks attached to each other.
      Named References ( head , curr , n ) = Ropes with hooks on the end, except they have anchors that keep them from floating away.
      If just one hook is pulled out, the blocks attached to it will drift away with the ocean's current.
      e.g) If the 'head' or anchor is pulled out from 4, then 4 and 12 will still be attached to each other, but they will drift away at sea.
      How can we attach our block with anchor 'n' between our blocks 4 and 12 without any blocks drifting away?
      Well, if we set curr.next ( rope between 4 and 12 ) to n's block ( 8 ), then 12 will float away because we're removing the hook holding on to 12 in order to attach it to n.
      Instead, we know 'n' is an anchor holding on to block 8, and n.next is a rope with a hook extending from block 8.
      n n.next
      | 8 |------------------->
      curr.next
      | 4 |-------------------> | 12 |
      If we have "n.next" hook on to 12 ( remember the only way to get 12 is through the rope "curr.next"), 12 will not drift away because our "n" is anchoring 8. So let's do that by:
      n.next = curr.next
      n n.next
      | 8 |----------------------\
      \ n.next
      curr.next \
      | 4 |-----------------------------------------------| 12 |
      Now, if we remove our rope "curr.next" that is hooking on to 12, then it's ok, because, n is anchoring 8 and 8 is connected to 12, so 12 will not float away.
      So let's do that by saying:
      curr.next = n
      n n.next
      | 8 |-------------------
      / \
      curr.next / \ n.next
      / \
      | 4 | | 12 |
      And that's basically it.
      Hope this helps :)

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

      @@BlueTreeCode beautiful
      Small suggestion
      Why are you not using memory also
      Instead of next we can use random memory also.
      For ex : for memory box 1 , reference is 100
      For memory box 2 , reference is 104
      It will be easy to understand.

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

      @@thanos9704 Hi Thanos, sure you can use numbers as memory addresses as a way of understanding how memory works. However, when it comes to writing out algorithms, 'next' is often used with Linked Lists, just as 'root', 'left' and 'right' are used Binary Trees. If you do take a class in C, however, you will definitely need to understand memory, as it isn't garbage collected like in Java. You will need to manually allocate and free memory.

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

    Beautiful video for sure... sadly I have to watch it multiple times to fully understand it .....

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

    Please do more videos we will be grateful to watch

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

      Thank you! I will try to push out some more videos, sruthi m.

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

    hi
    how do i can write a function that moves the linked list to the right k (variable) times? i.g: the linked list is : [ 1,2,3,4,5 ] and k=3 so the function returns [ 5,4,1,2,3 ]
    hope for a help

  • @MoTechq
    @MoTechq 7 місяців тому

    2024 here and the video is the fckng greatest explanation

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

    each code work in php in this video thanx u

    • @BlueTreeCode
      @BlueTreeCode  10 місяців тому

      I'm not familiar with php, but the logic should the same. Syntax, most likely not.

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

    For the AddAfter method, why curr = curr.next while curr is null? Is that mean if the curr is null, we switch curr into curr.next? but why? (sorry for the stupid question)

    • @BlueTreeCode
      @BlueTreeCode  5 років тому +2

      @Alex Yip, No question is stupid, my friend! That's how you learn. So let's go over the method. First, we check if curr, which pointing to the head of the list is null (simply as an error check) in the case that the Linked List is empty. And, well, if it's empty (null), that's great, because we have nothing to do, because we know that there's no data for us to insert after (therefore we never enter the while loop). However, if head is not null, then we definitely need to traverse the list to check if the node with that data (insertAfter) is in the list, so that we can insert after that node. In this case, we keep moving curr to the next node until we either reach the end of the list and find out the node we want to insert after is not there, or we find out that the node we want to insert after is there, and we insert our node. Hope this helps! :)

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

      @@BlueTreeCode Bro, you better my professor 10000 times

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

    Good explanation....

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

    @11:34 how can curr.data == data, because data is the data to be deleted

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

      Hey There! When I said 'data I want to remove', I meant to say 'data' I want to remove after. :) That's a wording mistake. 'data' is the data we want to remove after (in this case the data we want to remove after is 4, so we call deleteAfter(4)). Hope this cleared it up for you.

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

    Thanks for the great explanation.
    for the method deleteLast(); also after while(){} you should to invoke method deleteFirst() in case if we have one node
    // 6- deleteLast
    public void deleteLastNode() {
    if(isEmpty()) {
    System.out.println("List is Empty");
    return;
    }
    Node curr = head;
    Node nextNode = curr.next;

    while(curr.next != null) {
    if(nextNode.next == null) {
    curr.next = null;
    size--;
    }
    curr = nextNode;
    nextNode = nextNode.next;
    }

    deleteFirstNode();

    }

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

      Hi Ninoos. You're very welcome. So, the single node is taken care of in the conditional => if (curr == null || curr.next == null). In this conditional we set the head to null if there's a single node thereby removing it from the list.

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

    For the Add Node After challenge I did this, would it work? Because it's a bit different from yours:
    void InsertAfter(int after, int data){
    Node current = head;
    if (current != null){
    while (current.data != after){
    current = current.next;
    }
    Node n = new Node(data);
    current.next = n;
    }
    }

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

      Hi, Floruz. I'm glad you tried it, that's how you get better! So, the first thing that hits me is when the data is not in the list. When your loop gets to the last node and it sees that current.data is not equal to after, it will set current to its next (which is null). However, the loop doesn't stop, and then proceeds to check if current.data != after, and well, current is null so there can't be an after. Hope this helps :)

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

      @@BlueTreeCode Thank you!

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

    thank you

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

    thank you so much

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

    How does delete node after actually delete the node?

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

      Hi Tyrique, So in Java, once a node no longer a reference to it (or there's nothing pointing to it), it's automatically garbage collected and destroyed. This frees up memory that is no longer being used. Hope this helps :).

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

    i did this and its working for me but my problem is i have a 2d array linked list and im trying to delete tend of a column/row. and this is killing me lol

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

      Hi, Dang Le. If you meant a Linked List with each Node being a 2D array, then once you reach the node you want to remove data from, you can use a nested loop approach (if the data is not sorted) on the 2D array to search for and remove the element you're trying to delete by setting its value to Null (If you're dealing with a reference type). If you're dealing with a primitive type like an 'int' for example, you can set the value to an integer that would represent a false value for your particular problem (e.g. -1 or 0). If the data in the 2d array is sorted, you can most likely search for the data in (m+n) time instead of m*n, where m would be the # of rows and n would be the # of columns. Hope this helps :)

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

    Finally a video with not an Indian accent xD