美文网首页
1.9 合并两有序列表并保持有序

1.9 合并两有序列表并保持有序

作者: 就是果味熊 | 来源:发表于2020-03-06 16:15 被阅读0次

    2>4>6>8>10 1>3>5>7>9 -> 1>2>3>4>5>6>7>8>9>10

    LNode文件

    #%%
    from LNode import LNode, LHead,create_unordered_list
    
    #%%
    def merge_2_rise_head(head1,head2):
        if head1 == None or head1.next == None:
            return head2
        if head2 == None or head2.next == None:
            return head1
        
        cur1 = head1.next
        cur2 = head2.next
        head =LHead()
        cur = head
        while cur1 != None and cur2 != None:
            if cur1.data <= cur2.data:
                cur.next = cur1
                cur1 = cur1.next
            else:
                cur.next = cur2
                cur2 = cur2.next
            cur = cur.next
        if cur1 == None:
            cur.next = cur2
        if cur2 == None:
            cur.next = cur1
        return head
    
    list1 = [2,4,6,8,10]
    list2 = [0,1,2,3,5,7,9]
    head1 = create_unordered_list(list1)
    head2 = create_unordered_list(list2)
    
    head = merge_2_rise_head(head1,head2)
    list3 = head.get_ordered_list()
    print(list3)
    

    相关文章

      网友评论

          本文标题:1.9 合并两有序列表并保持有序

          本文链接:https://www.haomeiwen.com/subject/jwxwrhtx.html