merge_two_sorted_lists
1from utils import ListNode 2from typing import Optional 3 4 5# @leet start 6class Solution: 7 def mergeTwoLists( 8 self, list1: Optional[ListNode], list2: Optional[ListNode] 9 ) -> Optional[ListNode]: 10 """ 11 To merge two sorted lists, we first create a dummy node, and then refer 12 to it to iterate through the list. 13 and then, while both lists are non-null, we choose the lower value and 14 add it to the list. 15 Otherwise, one of the lists is non-empty, and if that's the case, we 16 append that to the end of our current list. 17 At the end, we return the next node of the list. 18 """ 19 head = ListNode() 20 curr = head 21 22 # then for each item in 23 while list1 and list2: 24 if list1.val < list2.val: 25 curr.next = list1 26 list1 = list1.next 27 else: 28 curr.next = list2 29 list2 = list2.next 30 curr = curr.next 31 32 if list1: 33 curr.next = list1 34 if list2: 35 curr.next = list2 36 37 return head.next 38 39 40# @leet end 41 42 43def test(): 44 assert 2 + 2 == 4
class
Solution:
7class Solution: 8 def mergeTwoLists( 9 self, list1: Optional[ListNode], list2: Optional[ListNode] 10 ) -> Optional[ListNode]: 11 """ 12 To merge two sorted lists, we first create a dummy node, and then refer 13 to it to iterate through the list. 14 and then, while both lists are non-null, we choose the lower value and 15 add it to the list. 16 Otherwise, one of the lists is non-empty, and if that's the case, we 17 append that to the end of our current list. 18 At the end, we return the next node of the list. 19 """ 20 head = ListNode() 21 curr = head 22 23 # then for each item in 24 while list1 and list2: 25 if list1.val < list2.val: 26 curr.next = list1 27 list1 = list1.next 28 else: 29 curr.next = list2 30 list2 = list2.next 31 curr = curr.next 32 33 if list1: 34 curr.next = list1 35 if list2: 36 curr.next = list2 37 38 return head.next
def
mergeTwoLists( self, list1: Optional[utils.ListNode], list2: Optional[utils.ListNode]) -> Optional[utils.ListNode]:
8 def mergeTwoLists( 9 self, list1: Optional[ListNode], list2: Optional[ListNode] 10 ) -> Optional[ListNode]: 11 """ 12 To merge two sorted lists, we first create a dummy node, and then refer 13 to it to iterate through the list. 14 and then, while both lists are non-null, we choose the lower value and 15 add it to the list. 16 Otherwise, one of the lists is non-empty, and if that's the case, we 17 append that to the end of our current list. 18 At the end, we return the next node of the list. 19 """ 20 head = ListNode() 21 curr = head 22 23 # then for each item in 24 while list1 and list2: 25 if list1.val < list2.val: 26 curr.next = list1 27 list1 = list1.next 28 else: 29 curr.next = list2 30 list2 = list2.next 31 curr = curr.next 32 33 if list1: 34 curr.next = list1 35 if list2: 36 curr.next = list2 37 38 return head.next
To merge two sorted lists, we first create a dummy node, and then refer to it to iterate through the list. and then, while both lists are non-null, we choose the lower value and add it to the list. Otherwise, one of the lists is non-empty, and if that's the case, we append that to the end of our current list. At the end, we return the next node of the list.
def
test():