From 26255473235e8545b782ebef05e1c0a5b36bc446 Mon Sep 17 00:00:00 2001 From: ITCharge Date: Mon, 13 May 2024 17:58:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=96=87=E4=B8=AD=E3=80=8C?= =?UTF-8?q?=E5=BB=BA=E7=AB=8B=E4=B8=80=E4=B8=AA=E7=BA=BF=E6=80=A7=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E3=80=8D=E7=9B=B8=E5=85=B3=E8=BF=87=E7=A8=8B=E3=80=81?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../01.Linked-List-Basic/01.Linked-List-Basic.md | 10 ++++++---- Templates/02.LinkedList/LinkedList.py | 6 ++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Contents/02.Linked-List/01.Linked-List-Basic/01.Linked-List-Basic.md b/Contents/02.Linked-List/01.Linked-List-Basic/01.Linked-List-Basic.md index 84ad3f7f..3f71376d 100644 --- a/Contents/02.Linked-List/01.Linked-List-Basic/01.Linked-List-Basic.md +++ b/Contents/02.Linked-List/01.Linked-List-Basic/01.Linked-List-Basic.md @@ -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 diff --git a/Templates/02.LinkedList/LinkedList.py b/Templates/02.LinkedList/LinkedList.py index a1bdc664..a3165e86 100644 --- a/Templates/02.LinkedList/LinkedList.py +++ b/Templates/02.LinkedList/LinkedList.py @@ -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