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
@@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.
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'.
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
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.
This earned a subscriber, ive been really struggling with this problem, appreciate your effort.
Thank you so much! i finally found a well explained video
Fantastic explanation, thank you!!
Thanks man appreciate your effort
I don't understand the cur = dummy business. At the end when you print dummy, why does that not print cur?
Awesome explanation, thank you
Nice Explanation 👍
what a explanation bro. sub done. carry on
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
Thanks bro, you so kind!
I have not found a video that explains line 6 and 7 of the code.
Cool stuff! Thank you!
Hey, thx for the explanation but in leetcode when i run the code i am getting run time limit exceeded
Thank you, a very nice explanation!
Why ListNode() is used in Sol
I have a question: How do I print the linked list that dummy.next produces? Ty in advance
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
@@imbesrs you sound like a stackoverflow hero
@@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!
@@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.
@@neerajkumar81 The person I replied to wanted to know how to print the output. I was not answering the original leetcode question
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?
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'.
initially dummynode is pointing to None,then with the condition it will point to head of l1 or l2
Consider dummy node is pointing to output linked-list
For me with the same code, Test case1 failed
Tysm 😭
👏🏽👏🏽👏🏽
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
youshould put (if then elif) not (two if)
why do we return dummy?
same Question :))
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.
@@neerajkumar81 thanks for explaining :)