以下是Python代码实现:

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
        
arr = [1, 2, 3]
head = ListNode()
cur = head

for num in arr:
    cur.next = ListNode(num)
    cur = cur.next

head = head.next # 去掉头结点

# 打印链表
while head:
    print(head.val, end=' ')
    head = head.next

输出:

1 2 3
将一个数组12 3赋值给一个单链表

原文地址: https://www.cveoy.top/t/topic/cvfj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录