python创建单链表的代码,存放数据1到100
时间: 2023-08-20 11:05:58 浏览: 133
python实现的单链表
以下是Python创建单链表并存放数据1到100的代码示例:
```python
# 定义单链表节点类
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
# 创建单链表
head = ListNode(0)
cur = head
for i in range(1, 101):
node = ListNode(i)
cur.next = node
cur = node
# 输出单链表
cur = head.next
while cur:
print(cur.val, end=' ')
cur = cur.next
```
运行结果:
```
1 2 3 4 5 ... 96 97 98 99 100
```
阅读全文