Leetcode - Merge Two Sorted Lists (Python)

preview_player
Показать описание
Leetcode Blind Curated 75
Leetcode - Merge Two Sorted Lists

Solving and explaining the essential 75 Leetcode Questions
Рекомендации по теме
Комментарии
Автор

Here is my solution finally i've solved a problem my own im so happy :D.

class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if not l1: return l2
if not l2 : return l1

if l1.val >= l2.val:
N = ListNode(l2.val, self.mergeTwoLists(l1, l2.next))

else:
N = ListNode(l1.val, self.mergeTwoLists(l1.next, l2))

return N

EzioOmer
Автор

That slack notification sound in the beginning gave me a mini heart attack

pratyaksh
Автор

Your videos have helped me learn so much. You are amazing.

raullopez
Автор

Thanks so much for explaining! I wasn't understanding why we had to return dummy.next. Peace.

Patiencelad
Автор

Hey guys. I'm stuck with something I cant grasp, why cant I access l1.value from the start of my code? I just wanted to check my l1's head current value and I get this error:

AttributeError: 'NoneType' object has no attribute 'val'

To me this does'nt make sense because I later access the lists 'val' attribute in the if statement. What am I getting wrong here?

alexanderkalen
Автор

Is this solution in o(1) space? You mentioned in the very end about an in-place solution but I thought this approach IS the in-place method?

Historyiswatching
Автор

Hi Tim, Great video! Could you explain why do I need to return the dummy.next instead of dummy, current or current.next? I am not particulary familiar with the syntax of listnode, any recommendation of material on this?

davidzorroyang
Автор

if l1.val < l2.val throws an error saying '<' not supported between instances of int and listnode. I am unable to figure out why you don't have the same error.

vipullade
Автор

Hey, really great video, helped me understand this a lot. Only thing I don't quite get is at the end when you write if l1: current.next = l1, how come this adds the whole rest of l1 to current.next when in the while loop when we add l1 to current.next it simply only adds one node? Thank you for the video man :)))

adventuredev
Автор

Can line 8 & 9 be removed ? It should be still working.

allielan
Автор

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?

rezayegane
Автор

res=list1 + list2
print(sorted(res)) Isn't it easier to write like this?

nurimrider
Автор

Could you please tell me what is the purpose of current = current.next in line 19.

kentsang
Автор

why cant you just

result= list1 + list2

result.sort()

zettkusanagi
Автор

I have never heard of a pointer I'm so lost any tips?

mageshiz
Автор

can ayone show me how to actually run this code

knox