Leet code practice
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(). ##create a new node
cur = dummy # point a variable to that new
while list1 and list2: #simpley put the list name which we are iterating onto
if list1.val > list2.val: #
cur.next = list2
list2 = list2.next
else:
cur.next = list1
list1 = list1.next ##point to the next one
cur = cur.next
if list1:
cur.next = list1
else:
cur.next = list2
return dummy.nextSteps
Last updated