数据结构 两个链表合并-python

来源:互联网 发布:miui8降级miui7数据 编辑:程序博客网 时间:2024/06/05 02:00
class Node():    def __init__(self, data=None, next=None):        self.data = data        self.next = nextn1 = Node(1, Node(3, Node(6, Node(7, Node(23, None)))))n2 = Node(4, Node(5, Node(8, Node(14, Node(34, None)))))def merge(n1,n2):    if not n1 and n2:        return n1 or n2    head = Node("begin",None)    cur = head    while n1 and n2:        if n1.data < n2.data:            cur.next = n1            cur = n1            n1 = n1.next        else:            cur.next =n2            cur = n2            n2 = n2.next    if n1:        cur.next = n2    else:        cur.next =n1    return head.nextn = merge(n1,n2)while n :    print(n.data)    n = n.next