Documentation IndexFetch the complete documentation index at: /llms.txtUse this file to discover all available pages before exploring further.
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Tested Python solution for LeetCode 21 with 14 pytest cases. Generate a practice environment with lcpy.
lcpy gen -n 21 # by problem number lcpy gen -s merge_two_sorted_lists # by problem name
list1
list2
Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4]
Input: list1 = [], list2 = [] Output: []
Input: list1 = [], list2 = [0] Output: [0]
[0, 50]
-100 <= Node.val <= 100
from leetcode_py import ListNode class Solution: # Time: O(m + n) # Space: O(1) def merge_two_lists( self, list1: ListNode[int] | None, list2: ListNode[int] | None ) -> ListNode[int] | None: dummy = ListNode(0) current = dummy while list1 and list2: if list1.val <= list2.val: current.next = list1 list1 = list1.next else: current.next = list2 list2 = list2.next current = current.next current.next = list1 or list2 return dummy.next