链表遍历解法
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def link(s): if not s: return None head = ListNode(s[0]) cur = head for val in s[1:]: cur.next = ListNode(val) cur = cur.next return head headA = link(list(map(int, input().strip().split()))) headB = link(list(map(int, input().strip().split()))) curA = headA curB = headB res = [] while curA or curB: if curA and (not curB or curA.val <= curB.val): res.append(str(curA.value)) curA = curA.next elif curB: res.append(str(curB.value)) curB = curB.next print(" ".join(res))
合并数组解法
l1 = list(map(int,input().split(" "))) l2 = list(map(int,input().split(" "))) #合并两个数组 l_res = l1 + l2 #对合并后数组整体升序排序 l_res.sort() #输出 print(" ".join(map(str,l_res)))