Skip to content

Commit

Permalink
更新文中「建立一个线性链表」相关过程、代码
Browse files Browse the repository at this point in the history
  • Loading branch information
itcharge committed May 13, 2024
1 parent d8789df commit 2625547
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,20 @@ class LinkedList:

> **建立一个线性链表**:根据线性表的数据元素动态生成链节点,并依次将其连接到链表中。
>
> 1. 从所给线性表的第 $1$ 个数据元素开始依次获取表中的数据元素
> 1. 从所给线性表中取出第 $1$ 个数据元素,建立链表头节点。然后依次获取表中的数据元素
> 2. 每获取一个数据元素,就为该数据元素生成一个新节点,将新节点插入到链表的尾部。
> 3. 插入完毕之后返回第 $1$ 个链节点的地址
> 3. 插入完毕之后返回第 $1$ 个链节点(即头节点)的地址
**「建立一个线性链表」** 的代码如下:

```python
# 根据 data 初始化一个新链表
def create(self, data):
self.head = ListNode(0)
if not data:
return
self.head = ListNode(data[0])
cur = self.head
for i in range(len(data)):
for i in range(1, len(data)):
node = ListNode(data[i])
cur.next = node
cur = cur.next
Expand Down
6 changes: 4 additions & 2 deletions Templates/02.LinkedList/LinkedList.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ def __init__(self):

# 根据 data 初始化一个新链表
def create(self, data):
self.head = ListNode(0)
if not data:
return
self.head = ListNode(data[0])
cur = self.head
for i in range(len(data)):
for i in range(1, len(data)):
node = ListNode(data[i])
cur.next = node
cur = cur.next
Expand Down

0 comments on commit 2625547

Please sign in to comment.