python 链表实现方式

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

def LinkedList(a):
    L = ListNode(a[0])
    l = L
    for i in range(1, len(a)):
        L.next = ListNode(a[i])
        L = L.next
    return l
def printl(a):
    out = []
    while a:
        out.append(a.val)
        a = a.next
    print(out)

a = [1,2,3,4]
l = LinkedList(a)
printl(l)
posted @ 2020-07-27 22:33  大超Zeo  阅读(165)  评论(0编辑  收藏  举报