在Python中将链接列表附加到另一个链接列表的末尾

时间:2015-11-24 10:47:29

标签: python linked-list

我无法在python上向另一个链接列表添加链接列表。 我认为逻辑应该是将一个列表的头部指向另一个列表的尾部。我的代码如下。

def append(listA, listB):
    if listA == None:
        return listB
    elif listB == None:
        return listA
    headA = listA
    headB = listB
    cA = headA
    cB = headB
    count = 0
    while cA is not None:
        count += 1
        cA = cA.next
    #Here I replace since cA is null, i set it equal to cB
    cA = push(cB, cB.data)
    return headA

我的测试代码是:

ten = Node(10)
seven = push(ten,7)
six = push(seven,6)
five = push(six,5)
four = push(five,4)

# ten = Node(10)
eleven = Node(11)
three = push(eleven,3)
two = push(three,2)
one = push(two,1)


print_list(one)
print_list(four)
append(one,four)
print_list(one)

1 个答案:

答案 0 :(得分:0)

Thansk的评论。我想问题的目的是创建一个追加函数来合并两个链表。我想通过指向错误的位置我只是犯了一个错误。我修改了我的代码如下,它的工作原理: Node类和push方法是:

"Time","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","2013","2378","2380","2758","3127","3128" 
"00:03:56","26","0","0","1","0","0","0","1953","22","39","8","5","269","5","0","0","0","1251","103","0","0","0","0","38","14","0","0","0","0","32","0","0","0" 
"00:09:16","22","0","0","2","0","0","21","48","0","4","4","7","382","12","0","0","0","1882","42","0","3","0","0","24","22","0","0","0","0","19","0","0","0"