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

合并两个排序的链表

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

# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param pHead1 ListNode类 
# @param pHead2 ListNode类 
# @return ListNode类
#
class Solution:
    def Merge(self , pHead1: ListNode, pHead2: ListNode) -> ListNode:
        # write code here
        #null
        if pHead1 is None:
            return pHead2
        if pHead2 is None:
            return pHead1
        
        cur1 = pHead1
        cur2 = pHead2
        if cur1.val <= cur2.val:
            head = cur1
            if cur1.next is None:
                head.next = cur2
                return head
            else:
                cur1 = cur1.next
                n = cur2
        else:
            head = cur2
            if cur1.next is None:
                head.next = cur2
                return head
            else:
                cur2 = cur2.next
                n = cur1
        
        head0 = head

        while n:
            if cur1.val <= cur2.val:
                if cur1.next is None:
                    head.next = cur1
                    cur1.next = cur2
                    return head0
                else:
                    tem = cur1.next
                    head.next = cur1
                    head = cur1
                    cur1 = tem
            else:
                if cur2.next is None:
                    head.next = cur2
                    cur2.next = cur1
                    return head0
                else:
                    tem = cur2.next
                    head.next = cur2
                    head = cur2
                    cur2 = tem
            
        return head0


            

        

超级笨办法

全部评论

相关推荐

白火同学:1、简历可以浓缩成一页,简历简历先要“简”方便HR快速过滤出有效信息,再要“历”用有效信息突出个人的含金量。 2、教育背景少了入学时间~毕业时间,HR判断不出你是否为应届生。 3、如果你的平台账号效果还不错,可以把账号超链接或者用户名贴到对应位置,一是方便HR知道你是具体做了什么内容的运营,看到账号一目了然,二是口说无凭,账号为证,这更有说服力。
面试被问期望薪资时该如何...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务