您的位置:首页 > 理论基础 > 数据结构算法

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

2017-06-27 17:28 471 查看
class Node():
def __init__(self, data=None, next=None):
self.data = data
self.next = next

n1 = 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.next
n = merge(n1,n2)
while n :
print(n.data)
n = n.next


4000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: