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