题解 | #合并两个排序的链表#

合并两个排序的链表

https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337

class Solution:
    def Merge(self , pHead1: ListNode, pHead2: ListNode) -> ListNode:
        # write code here
        head = ListNode(None)
        rear = head
        while pHead1 and pHead2:
            if pHead1.val < pHead2.val:
                temp = pHead1.next
                rear.next = pHead1
                rear = pHead1
                pHead1 = temp
            else:
                temp = pHead2.next
                rear.next = pHead2
                rear = pHead2
                pHead2 = temp
        while pHead1:
            temp = pHead1.next
            rear.next = pHead1
            rear = pHead1
            pHead1 = temp
        while pHead2:
            temp = pHead2.next
            rear.next = pHead2
            rear = pHead2
            pHead2 = temp
        return head.next

全部评论

相关推荐

10-28 15:45
门头沟学院 C++
西南山:海康威视之前不是大规模裁员吗
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务