21. Merge Two Sorted Lists Easy
Python Code
1 class Solution:
2 def mergeTwoLists(self, list1, list2):
3 dummy = ListNode(0)
4 current = dummy
5
6 while list1 and list2:
7 if list1.val <= list2.val:
8 current.next = list1
9 list1 = list1.next
10 else:
11 current.next = list2
12 list2 = list2.next
13 current = current.next
14
15 current.next = list1 if list1 else list2
16
17 return dummy.next
Visualization
List 1
List 2
Merged List
List1 Val
-
List2 Val
-
Selected
-
Initialize dummy node and current pointer
0 / 0
Step Log