鏈結串列(Linked list)
文章目錄
介紹
鏈結串列是線性的數據結構(linear collection)
,與陣列不同,鏈結的元素不存儲在連續位置,元素使用指針鏈接。它們包括一系列連接的節點。每個節點存儲數據和下一個節點的位置(如下圖所示),便於追加或刪除,但儲存數據很費時。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
優缺點
優點
- 新增以及刪除資料比陣列來的更快
- 動態陣列,不會有陣列重新定義大小問題
缺點
- 數據儲存不連續記憶體位置
- 訪問指定位置必須從頭開始查找
- 需而外記憶體空間儲存儲存指標
複雜度
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
(圖片取自於bigocheatsheet)
時間複雜度
動作 |
時間複雜度 |
插入 |
/ 插入頭節點 |
刪除 |
/ 刪除頭節點 |
搜尋 |
|
訪問index |
|
空間複雜度
其中 n
是列表中元素的數量,因為列表中的每個節點都需要內存來存儲其數據和指向下一個節點的指針。
常見種類
- 單向鏈結串列(Singly Linked List)
- 雙向鏈結串列(Doubly Linked List)
- 迴圈鏈結串列(Circularly Linked List)
單向鏈結串列(Singly Linked List)
每兩個節點間只有一個單向的鏈結。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
(圖片取自於types-of-linked-list)
雙向鏈結串列(Doubly Linked List)
帶有兩個指標指向前後數據,缺點是必須增加數據儲存領域,追加數據以及刪除數據時,要變更方向的指標也變多了。
- 查找值為
Singly Linked List
一半時間(平均)
- 可從前往後讀取,或從後往前讀取
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
(圖片取自於types-of-linked-list)
迴圈鏈結串列(Circularly Linked List)
沒有頭尾概念,用於想維持最新數據為固定數量時。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
(圖片取自於types-of-linked-list)
實作鏈結串列
單向鏈結串列(Singly Linked List)
以下使用 JavaScript
實作
建立Node
建立鏈結串列
新增push
方法
將新節點新增至末端
- 建立
Node
,value
為傳入的值
- 如果head為空,則將
head
設置為新節點
- 否則,使用一個
while
循環,遍歷Linked List
直到找到最後一個節點
- 最後一個節點的
next
指向新節點
- 增加
Linked List
的長度
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
(圖片來自於visualgo)
新增pop
方法
移除尾端節點
- 如果
head
為空,則返回null
- 如果鏈表長度為
1
,則將head
設置為null
並返回原本的head
節點
- 否則,使用一個
for
循環,遍歷Linked List
直到找到倒數第二個節點
- 將倒數第二個節點的
next
設置為null
- 減少
Linked List
長度,並返回尾端節點
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
(圖片來自於visualgo)
新增shift
方法
刪除開頭的節點
- 如果
head
為空,則返回null
- 如果
Linked List
長度為1
,則將head
設置為null
並返回原本的head
節點
- 否則,將
head
設置為head
的下一個節點
- 減少
Linked List
長度,並返回原本的head節點

(圖片來自於visualgo)
新增unshift
方法
新增新節點在頭部
- 如果
head
為空,則將head
設置為新節點。
- 否則,創建一個新節點,將
head
設置為新節點,並將新節點的next
指向原本的head
節點。
- 增加
Linked List
的長度

(圖片來自於visualgo)
新增insertAt
方法
在Linked List
中的特定索引位置新增一個新節點
- 如果索引大於
Linked List
的長度或小於0
,則返回null
。
- 如果索引等於
0
,則通過unshift
方法在Linked List
開頭添加一個新節點。
- 如果索引等於
Linked List
的長度,則通過push
方法在Linked List
末尾添加一個新節點。
- 否則,創建一個新節點,遍歷到索引位置的節點,將新節點的
next
指向當前節點的下一個節點,並將當前節點的next
指向新節點。
- 增加
Linked List
的長度

(圖片來自於visualgo)
新增removeAt
方法
在Linked List
中的特定索引位置刪除一個節點
- 首先檢查指定的索引是否在
Linked List
的範圍之外,在這種情況下返回null
- 如果索引為
0
則使用shift
方法刪除頭節點,返回刪除節點
- 如果索引等於
Linked List
長度則使用pop
方法刪除尾端節點並返回
- 否則,遍歷
Linked List
找到要移除的索引位置減一,更新指針已移除目標節點,並返回刪除節點
- 減少
Linked List
的長度

(圖片來自於visualgo)
新增get
方法
返回索引位置的值
- 檢查索引是否大於或等於
Linked List
的長度或小於 0
,在範圍外則返回null
- 範圍內則迭代至
index
位置則返回

(圖片來自於visualgo)
完整程式碼

(圖片取自於GeeksforGeeks)

(圖片取自於GeeksforGeeks)
實戰
707. Design Linked List
題目說明
設計一個 Linked list
可以選擇使用單向鏈結或雙向鏈結,單向鏈結的 node
須包含 val
和 next,而雙向鏈結則需再多一個 prev
屬性。

code
詳細解法
參考來源
資料結構與演算法 (JavaScript)