Merge Two Sorted Lists

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ส.ค. 2024

ความคิดเห็น • 109

  • @spectermakoto9029
    @spectermakoto9029 4 ปีที่แล้ว +24

    they got me with the term splicing them together I didn't know I could have made a new list

    • @xSlimLizardx
      @xSlimLizardx 2 ปีที่แล้ว

      bro same here, i was out here with a million pointers trying to merge them into one lmao

  • @g_455
    @g_455 4 ปีที่แล้ว +8

    @Kevin
    Thanks for making this video.
    Just a quick thing to point the time complexity in the worst case will be O(M+N) as we may have to iterate all the values in both the lists, this happens when the values in the lists are of same range.

    • @jameslemieux8973
      @jameslemieux8973 3 ปีที่แล้ว

      Yea I was just about to mention that. I think the test data on this question is fairly weak imo

    • @daemperador123
      @daemperador123 3 ปีที่แล้ว +1

      At the end that will still be O(N)

  • @eminen758
    @eminen758 4 ปีที่แล้ว +16

    Got this question on a Facebook interview, happy to see I used the same approach as you!

    • @drywater3729
      @drywater3729 3 ปีที่แล้ว +2

      Did you get it?!

    • @bosteador
      @bosteador 3 ปีที่แล้ว

      Did they mention the recursive approach or nah?

    • @eminen758
      @eminen758 3 ปีที่แล้ว

      @@drywater3729 I chose an offer from the Bank of England instead

    • @eminen758
      @eminen758 3 ปีที่แล้ว

      @@bosteador Yeah they don't mention it explicitly - they just sort of let you do your own thing and ask you questions as to why you're doing what you're doing and how you could improve it etc.

    • @j81enrose43
      @j81enrose43 3 ปีที่แล้ว

      You got asked an easy question at facebook? Although leetcode has some 'hard' easy problems, I was expecting much more advanced/sophisticated than this question

  • @rydmerlin
    @rydmerlin ปีที่แล้ว

    Alternatively you can start with an empty list and conditionally make your first node the head.

  • @TKollaKid
    @TKollaKid 2 ปีที่แล้ว +1

    I found it tougher if you cannot create a new list but merge the two in place and return only one of the lists. I saw that in an interview too.

  • @suryanshrajdubey
    @suryanshrajdubey 5 ปีที่แล้ว +5

    Your explanations are awesome. Thanks for the video man!! Love from India. :D

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว

      Anytime Suryansh and thank YOU for the support!!! :)

  • @ivanchl
    @ivanchl 3 หลายเดือนก่อน

    The explanations are really good. Thanks!!

  • @safiahasansyed6625
    @safiahasansyed6625 4 ปีที่แล้ว +4

    @Kevin, Really like your coding walk through style! Could you please add to your list some of Leetcode problems: Longest Palindrome Substring(#5), Minimum Window Subsequence(#727), Minimum Window Substring(#76), Longest continuous increasing subsequence(#674). I know its a huge list but would really like to see your thought process :) BTW, problem explanation is so good. Thanks!

  • @greenElement
    @greenElement 4 ปีที่แล้ว +5

    Do you get compliments on your eyes? They're nice. I had to share, what use is it keeping it to myself

  • @iphoneeating
    @iphoneeating 5 ปีที่แล้ว +5

    Keep up the good work. You make it look so easy.

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว +1

      IPhone Apple thanks! And these questions are hard don't be fooled!!! It takes a lot of practice!

  • @sakkamouid3343
    @sakkamouid3343 ปีที่แล้ว

    Hi ! My question is only about this last part :
    if(l1!=null){
    dummy.next=l1;
    }
    if(l2!=null){
    dummy.next=l2;
    }
    Doesn't this block of code logically take care of the numbers which haven't been added in the while loop ?
    For example our lists are : list1=[1,2,4] list2=[1,3,4,7,8,9]
    So the while loop will form the list=[1,1,2,3,4,4] and the rest will be added by the block :
    if(l2!=null){
    dummy.next=l2;
    }
    My question is : doesn't that block add only one node ? Because it would be able to add multiple nodes . It would be in my opinon something like this :
    while(l2!=null){
    dummy.next=l2;
    l2=l2.next;
    }
    What is the logic mistake that I made ? Thank you in advance!

  • @aaryanbudhirajamusic6874
    @aaryanbudhirajamusic6874 4 ปีที่แล้ว +5

    I was not able to do this on my own, i am not sure but it doesn't look that tough but I was still not able to :/. Guilty Feels max lmao
    PS: Thanks for the explanation!

  • @harisahan2126
    @harisahan2126 4 ปีที่แล้ว +1

    seriously, man you nailed it. so helpful

  • @backistall3452
    @backistall3452 2 ปีที่แล้ว +1

    HELL NO. IF AMAZON REALLY ASKED THIS, I WOULD BE THE NEW CEO

  • @alexirabor
    @alexirabor 2 ปีที่แล้ว

    Both the time and space complexity will be O(n+m). n represents the number of elements in L1 and m represents the elements in L2. Because we are creating extra space, the space complexity will also be O(n + m)

    • @Jimmy-ng3ci
      @Jimmy-ng3ci 2 ปีที่แล้ว +1

      I believe the time complexity should be O(min(n,m)) whichever list is smallest determines the loop range after that we exit and just set the result node next to the remaining list node

  • @szhang0119
    @szhang0119 4 ปีที่แล้ว +4

    Great video, Kevin! I have a question regarding the runtime analysis though. I think what you described in the video is correct, but the caption is wrong. The runtime complexity should be O(M+N) in the worst case, where M, N are the lengths of the two list. If the two lists have smaller values alternatively (e.g. one is 1->3->5->7, while the other one is 2->4->6->8), then you have to iterate through both lists.

    • @kishorekumar-wx5vc
      @kishorekumar-wx5vc 4 ปีที่แล้ว +2

      Thanks mate, i was so confused on the Time complexity of this problem. you made it clear!!

  • @TheAntloo
    @TheAntloo 4 ปีที่แล้ว +1

    You give the best explanations! Thank you so much!

  • @cassidees7708
    @cassidees7708 2 ปีที่แล้ว

    if these ListNodes were pointers and we created dummy = new ListNode(-1); then would it make sense to delete that dummy node from memory before returning head->next?

  • @ryanmckay8451
    @ryanmckay8451 3 ปีที่แล้ว +1

    thanks so much! I couldn't understand the solutions on leetcode, but this made it click

  • @nishant5519
    @nishant5519 4 ปีที่แล้ว

    Hey Kevin, I like the way you explain your approach and then how you convert that in code. Great

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 ปีที่แล้ว +1

      nishant singh thanks Nishant! If you like my explanations be sure to join the interviewing service I created thedailybyte.dev/?ref=kevin I recommend joining a premium plan if you can!

    • @nishant5519
      @nishant5519 4 ปีที่แล้ว

      @@KevinNaughtonJr Yeah...sure...will have a look

  • @buzzolol
    @buzzolol ปีที่แล้ว

    Why sometimes there are lists left? If the while uses && shouldn't stop only when both are null?

  • @nosoyyotampoco
    @nosoyyotampoco 2 ปีที่แล้ว

    I really enjoy watching videos to get more knowledge on coding.

  • @sigma_animation
    @sigma_animation 3 ปีที่แล้ว

    Bro your videos are fire, so helpful, thank you so much

  • @playstation243
    @playstation243 2 ปีที่แล้ว

    Dumb question, but which part of the code takes care of that case where l1 = l2?

  • @augustoalmeida2586
    @augustoalmeida2586 3 ปีที่แล้ว

    great video bro! thank you for taking the time to explain the logic behind ur algorithm

  • @poojaguru2516
    @poojaguru2516 5 ปีที่แล้ว +2

    Your videos are really helpful !! Thank you Kevin....keep doing 😋

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว

      Thanks Pooja and don't worry I will :)

  • @MikaylaJoyBotha
    @MikaylaJoyBotha 3 ปีที่แล้ว

    This is a great video! Thanks. How do you know to set it equal to null?

  • @OliverJanShD
    @OliverJanShD 5 ปีที่แล้ว

    Thanks for the video! It's very useful to hear your explanations during the problem

  • @rezayegane7708
    @rezayegane7708 2 ปีที่แล้ว

    Hi. I know about reference types (in Java or C#) but I don't understand how `dummy` get its value. what topics should I read about to know how its works?

  • @prabhnoorsinghchawla3190
    @prabhnoorsinghchawla3190 5 ปีที่แล้ว +2

    Hi Kevin! Love your channel

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว

      Anytime Prabhnoor, thanks for the support. I'll put that one on my list :)

  • @landrykezebouyankam8749
    @landrykezebouyankam8749 5 ปีที่แล้ว +2

    Hi Kevin! Great videos! Thank you so much for your explanations.
    I'm a bit confused though. So on line 15 and line 18 when you do : dummy.next =l1 (and l2 respectively), does it not append everything left in list L1 or L2 to the dummy list (just as it would in line 25 and 27 respectively?)

    • @abhaymarathe2960
      @abhaymarathe2960 4 ปีที่แล้ว

      I have the same doubt. I think to add all remaining elements in l1 or l2, we will have to use one more while loop. It would be great if Kevin clarifies it.

    • @euanmorgann
      @euanmorgann 4 ปีที่แล้ว +12

      Hello, L1 isn't a value, it is a node. A node has references to each other item in the list. At the beginning when we loop through and put items in individually, that happens because we splice in other nodes after them which changes the references. At the end, where we just put in either L1 or L2, the references remain intact, so all the remaining values get put into the list as well. Hope that helps!

    • @ramr3
      @ramr3 4 ปีที่แล้ว

      Did you ever find a good explanation to this?

    • @kurtrusselferrer5424
      @kurtrusselferrer5424 4 ปีที่แล้ว +1

      Any of you guys know why the need to put next to dummy.next for the first value instead of just putting dummy? I am really confused. :((

  • @Rahulyadav-lv7dh
    @Rahulyadav-lv7dh 3 ปีที่แล้ว

    I didn't get why is && statement used in the loop instead of || ,is it necessary for both the lists to get null

  • @shenth27
    @shenth27 ปีที่แล้ว

    What'sthe point of head variable? can't we just return dummy.next at the end

  • @oneilcontractor8537
    @oneilcontractor8537 4 ปีที่แล้ว

    Hey Kevin!! Big Fan of your videos and you've helped me a lot while preparing for my interviews, can you please make a video on 'Reverse Nodes in k-Group' Leetcode problem?

  • @chaveyvillegas
    @chaveyvillegas 4 ปีที่แล้ว +1

    Hi Kevin. Great video. I love how you explain these problems. I do have one question though. I am a second year CS student who just learned about linked lists. In my class we created a new node object every time we added to our linked list. So for example, if we had 5 items in our linked list there would be 5 objects each pointing to the previous object in the list. It seems like in your code you are just changing the values in your dummy list without creating new objects. I was wondering if you could explain how the dummy list is being appended in your code without creating new objects?

    • @PureForloko
      @PureForloko 3 ปีที่แล้ว +4

      It's been almost a year so I'm sure you understand this now but in case anyone else had the same question:
      Your teacher created multiple new Nodes because he was starting from scratch. In this case we are given Nodes to work with.
      So if we work with the same scenario that Kevin has in this video, if have two ListNodes being passed into our function. These two ListNodes can look something like:
      l1 = 1 -> 2 -> 3
      l2 = 1 -> 3 -> 4
      Keep in mind that although we are just showing numbers here you should really think of the numbers encased in a 'ListNode' object.
      When we create a new dummy ListNode in our code:
      ListNode dummy = new ListNode(-1), we now have something that looks like this:
      -1 -> null
      Now our next variable in our ListNode class holds a ListNode object. So if you want to assign a value to our next variable you can do something like
      l1.next = new ListNode(5) (We're creating a new ListNode object and assigning our next variable to it)
      However we also have our other ListNodes, l1, and l2, these are ListNode objects that have already been instantiated (created).
      So we know our next variable holds ListNode objects and l1 which currently holds the value 1 is a ListNode object so we can just go
      dummy.next = l1; (since l1 is a ListNode object)
      So now dummy looks like
      -1 -> 1 -> 2 -> 3

    • @Sunshine-mj2pf
      @Sunshine-mj2pf 2 ปีที่แล้ว

      @@PureForloko Thanks this is super helpful! I am also confused by this but you answer all the questions I have.

    • @PureForloko
      @PureForloko 2 ปีที่แล้ว

      @@Sunshine-mj2pf glad to hear it man!

  • @queenb3730
    @queenb3730 ปีที่แล้ว

    how do i get the the length of the listnode l1 for example?

  • @sju9227
    @sju9227 5 ปีที่แล้ว

    I would have preferred if you wouldnt change the original node and practice immutability

  • @nbavideos4487
    @nbavideos4487 2 ปีที่แล้ว

    ty bro

  • @chrisramo1
    @chrisramo1 5 ปีที่แล้ว

    Awesome, nice explanation! Ty

  • @salarystealer
    @salarystealer 3 ปีที่แล้ว

    Thanks

  • @seal0118
    @seal0118 4 ปีที่แล้ว

    what happens if have one valid list and the other is a null pointer ? how does dummy node help in this case ??

  • @yolopassion
    @yolopassion 4 ปีที่แล้ว

    Can't we use the solution you have mentioned in K sorted lists that use min-heap?

  • @vishalsingh-pg6em
    @vishalsingh-pg6em 3 ปีที่แล้ว +2

    Your approach is correct but I guess in the final if-else block where we check if there are any more elements left in either of the two lists, there we need to use while loop inside our if / else block to check how many more elements are still left either in list1 or list2 after one of the list reaches to null in our upper while loop.
    And we also need to move dummy = dummy.next in the bottom if-else block along with the l1 = l1.next || l2 = l2.next

    • @sakkamouid3343
      @sakkamouid3343 ปีที่แล้ว

      That's the same thing I just asked about .
      Here was my comment :
      Hi ! My question is only about this last part :
      if(l1!=null){
      dummy.next=l1;
      }
      if(l2!=null){
      dummy.next=l2;
      }
      Doesn't this block of code logically take care of the numbers which haven't been added in the while loop ?
      For example our lists are : list1=[1,2,4] list2=[1,3,4,7,8,9]
      So the while loop will form the list=[1,1,2,3,4,4] and the rest will be added by the block :
      if(l2!=null){
      dummy.next=l2;
      }
      My question is : doesn't that block add only one node ? Because it would be able to add multiple nodes . It would be in my opinon something like this :
      while(l2!=null){
      dummy.next=l2;
      l2=l2.next;
      }
      What is the logic mistake that I made ? Thank you in advance!

  • @shwetabhshekhar1282
    @shwetabhshekhar1282 3 ปีที่แล้ว

    Best explanation out there.

  • @jaimemiranda6520
    @jaimemiranda6520 5 ปีที่แล้ว +1

    You should do a Kevin Naughton x CS Dojo collab

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว

      Haha I'd love to that'd be so cool!!!

    • @sujithns5033
      @sujithns5033 4 ปีที่แล้ว

      Plus one! Thanks a lot for the videos Kevin! :)

  • @victorgaina8383
    @victorgaina8383 4 ปีที่แล้ว

    Why is he returning head.next? What is head.next pointing to?

  • @kuting_slayer
    @kuting_slayer 3 ปีที่แล้ว

    Wait, this solution also modifies the order of the l1 and l2 to become the merged list

  • @chenchangyuan6551
    @chenchangyuan6551 4 ปีที่แล้ว

    Thank you kevin.

  • @darod6098
    @darod6098 4 ปีที่แล้ว +1

    6:10 6.10 while is this an if and not a while?

    • @victorgaina8383
      @victorgaina8383 4 ปีที่แล้ว

      I'm confused by that as well

    • @fax160
      @fax160 4 ปีที่แล้ว +1

      You only need to append the one node, because the one node references the remaining nodes.

  • @lugiadark21
    @lugiadark21 3 ปีที่แล้ว +1

    I just got asked this in an interview

    • @unknownman1
      @unknownman1 3 ปีที่แล้ว +2

      how did it go?

    • @lugiadark21
      @lugiadark21 3 ปีที่แล้ว

      @@unknownman1 I had never seen this problem, but luckily my interviewer was nice and guided me to the right answer.

  • @anyadavidovich1083
    @anyadavidovich1083 4 ปีที่แล้ว

    missing return statement or is it just me ? it should return dummy.next, correct?

    • @anyadavidovich1083
      @anyadavidovich1083 4 ปีที่แล้ว

      rather, it should return head.next ?

    • @jakeguthrie7230
      @jakeguthrie7230 4 ปีที่แล้ว

      @@anyadavidovich1083 Yep, he got that at 7:53

  • @cnaught0802
    @cnaught0802 5 ปีที่แล้ว

    So helpful!

  • @TheMcallist1
    @TheMcallist1 5 ปีที่แล้ว

    Thanks Kevin!

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว +1

      Anytime Thomas, thanks for the support!

  • @indiansoftwareengineer4899
    @indiansoftwareengineer4899 5 ปีที่แล้ว

    Who the fuck gave one dislike?
    I don't know why people are so mean?
    BTW, nice video Sir.

  • @bharathik6479
    @bharathik6479 4 ปีที่แล้ว

    bro please make a video for this problem.
    138. Copy List with Random Pointer

  • @anindita2816
    @anindita2816 5 ปีที่แล้ว

    You're awesome!

  • @didoma73
    @didoma73 3 ปีที่แล้ว

    i don't understand how head.next is not null? When was head.next ever assigned to?

  • @baburaomulaparthi4531
    @baburaomulaparthi4531 3 ปีที่แล้ว

    Bro
    Can u provide source code link

  • @digisecureagent7679
    @digisecureagent7679 2 ปีที่แล้ว

    Easier solution:
    if (head1.data < head2.data) {
    head1.next = mergeSortMethod(head1.next, head2);
    return head1;
    } else {
    head2.next = mergeSortMethod(head1, head2.next);
    return head2;
    }

    • @O13x11
      @O13x11 2 ปีที่แล้ว

      Can you elaborate what mergesortmethod(head1.Next,head2) is doing? Like i put two nodes as arguments and recieving one node, but which one?

  • @mahir7530
    @mahir7530 2 ปีที่แล้ว

    why return head.next

  • @aiyazsattar8089
    @aiyazsattar8089 5 ปีที่แล้ว +2

    You have used && , so it comes out only if both the condition is false but the if condition will be false because when we are out of the while loop both the l1 and l2 will be null, I guess you should have used || condition

    • @aronblanche
      @aronblanche 4 ปีที่แล้ว +1

      You can also do or operation, but suppose the smaller list got exhausted in while loop, then you can simply append the remaining big list in single operation to improve performance. Never doubt Kevin naughton

  • @SraddhanjaliAcharyatilaprimera
    @SraddhanjaliAcharyatilaprimera 4 ปีที่แล้ว

    What if l1.val == l2.val, it seems you are only checking less than or greater than

    • @nidhidesai3502
      @nidhidesai3502 4 ปีที่แล้ว

      Exactly my question. I believe need to add - elseif (and thereby third condition all together ) if values are same you want to move to pointer for both nodes to next one and also add both values to the third list generated.. -> that way it might give more accurate results.

    • @SraddhanjaliAcharyatilaprimera
      @SraddhanjaliAcharyatilaprimera 4 ปีที่แล้ว

      @@nidhidesai3502 I think the equality part is handled by the else clause as well as for greater than check.

    • @nidhidesai3502
      @nidhidesai3502 4 ปีที่แล้ว

      @@SraddhanjaliAcharyatilaprimera - correct .. else clause does not have condition specified so it will handle all clauses.. and we dont need to move both the pointer in forward direction since we need all the values in final array ! thanks for pointing that out !

  • @varunkulkarni2058
    @varunkulkarni2058 3 ปีที่แล้ว

    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
    if(l1==NULL) return l2;
    if(l2==NULL) return l1;
    ListNode* dummy;
    if(l1->valval){
    dummy=l1;
    dummy->next=mergeTwoLists(l1->next,l2);
    }
    else{
    dummy=l2;
    dummy->next=mergeTwoLists(l1,l2->next);
    }
    return dummy;
    }

  • @JanacMeena
    @JanacMeena 3 ปีที่แล้ว

    Thx for another great video kevin. Written explanation here: medium.com/@7anac/coding-interview-question-merge-two-sorted-lists-leetcode-21-fb2b166b5

  • @VVV-xf5vl
    @VVV-xf5vl ปีที่แล้ว

    vvv

  • @millenialmusings8451
    @millenialmusings8451 2 ปีที่แล้ว +1

    what an idiotic video.. started explaining the content way to late and no visuals to go thru.. downvoting