Merge Two Sorted Lists | Leet code 21 | Theory explained + Python code

Поделиться
HTML-код
  • Опубликовано: 15 янв 2025

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

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

    This earned a subscriber, ive been really struggling with this problem, appreciate your effort.

  • @anasbhih5114
    @anasbhih5114 11 месяцев назад

    Thank you so much! i finally found a well explained video

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

    Fantastic explanation, thank you!!

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

    Thanks man appreciate your effort

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

    I don't understand the cur = dummy business. At the end when you print dummy, why does that not print cur?

  • @ayeshaarifkhawaja-nz4xt
    @ayeshaarifkhawaja-nz4xt Год назад

    Awesome explanation, thank you

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

    Nice Explanation 👍

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

    what a explanation bro. sub done. carry on

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

    how did the program know that we only took the first initial value of l1 or l2?
    why don't we just code like this: curr.next = l2.val ?? Ty in advance

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

    Thanks bro, you so kind!

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

    I have not found a video that explains line 6 and 7 of the code.

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

    Cool stuff! Thank you!

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

    Hey, thx for the explanation but in leetcode when i run the code i am getting run time limit exceeded

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

    Thank you, a very nice explanation!

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

    Why ListNode() is used in Sol

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

    I have a question: How do I print the linked list that dummy.next produces? Ty in advance

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

      You'd have to iterate over the entire linked list. Its the one downside of linked list.
      class node:
      def __init__(self, data=None):
      self.data = data
      self.next = None
      def display(self):
      elems = [ ]
      cur_node = self
      while cur_node.next != None:
      cur_node = cur_node.next
      elems.append(cur_node.data)
      print(elems)
      This method you add them all to an array and print the array.
      You could also do string concatenation or print at the end of each while loop iteration

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

      @@imbesrs you sound like a stackoverflow hero

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

      @@dudfetbird I appreciate that but im very new to coding as well. Just now learning about linked list so the knowledge wsa fresh in my head. Cheers!

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

      @@imbesrs `elems` here is a flat list, the problem needs a linked list to be output. Meaning, you need to manage the next pointers appropriately in the final result.

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

      @@neerajkumar81 The person I replied to wanted to know how to print the output. I was not answering the original leetcode question

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

    I have one question:- Initially the Dummy node is pointing to the head node (I got it) but which head node? Head node of L1 or head node of L2?

    • @AbhishekSharma-vb9ux
      @AbhishekSharma-vb9ux 3 года назад +1

      I think dummy node is an independent node with val=0 and next = none. It doesnt point to any head initially, and after the first comparison, it points to '1'.

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

      initially dummynode is pointing to None,then with the condition it will point to head of l1 or l2

    • @HarshPatel-iy5qe
      @HarshPatel-iy5qe Год назад

      Consider dummy node is pointing to output linked-list

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

    For me with the same code, Test case1 failed

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

    Tysm 😭

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

    👏🏽👏🏽👏🏽

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

    pulling my hair because of this
    why does the following for the while loop part not work? why do i need the else statement?
    while list1 and list2:
    if list1.val > list2.val:
    print(list2.val)
    tail.next = list2
    list2 = list2.next
    if list1.val

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

    why do we return dummy?

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

      same Question :))

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

      Initially dummy points to curr. But, later in the loop - curr moved on to create the resultant list. Finally, dummy still points to an address which was held by curr at the very beginning. So, while returning dummy, we are making sure, dummy gets the head pointer of the resultant list.

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

      @@neerajkumar81 thanks for explaining :)