尾插法建立单链表(Python版)
习惯了力扣模式,碰到acm模式让手动建立单链表就不会了。记录一下吧。
class ListNode:def __init__(self, val=0, next=None):self.val = valself.next = nextdef createList(nums): #输入一个数组,返回链表头结点prehead = ListNode()p = preheadfor item in nums:node = ListNode(item)p.next = nodep = nodereturn prehead.nexthead = createList([1, 2, 3])
while head:print(head.val, end=' ')head = head.next

本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
