题解 | #链表合并#

链表合并

https://www.nowcoder.com/practice/46bda7f0570a47b6b54a29a0a6ae4c27

import sys


class ListNode:
    def __init__(self, value=0, next=None):
        self.value = value
        self.next = next


def parse_input(slist):
    if not slist:
        return None
    head = ListNode(slist[0])
    curr = head
    for value in slist[1:]:
        curr.next = ListNode(value)
        curr = curr.next
    return head


a = list(input().strip().split())
b = list(input().strip().split())
headA = parse_input(a)
headB = parse_input(b)

res = []
curA = headA
curB = headB

while curA or curB:
    if curA and (not curB or curA.value <= curB.value):
        res.append(curA.value)
        curA = curA.next
    elif curB:
        res.append(curB.value)
        curB = curB.next

print(" ".join(map(str,res)))

全部评论

相关推荐

2024-11-28 21:33
广东工业大学 Java
点赞 评论 收藏
分享
2024-12-05 15:53
中南大学 Java
点赞 评论 收藏
分享
01-17 15:55
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务