|
| 1 | +# 链表 |
| 2 | + |
| 3 | +在计算机科学中, 一个 **链表** 是数据元素的线性集合, 元素的线性顺序不是由它们在内存中的物理位置给出的。 相反, 每个元素指向下一个元素。它是由一组节点组成的数据结构,这些节点一起,表示序列。 |
| 4 | + |
| 5 | +在最简单的形式下,每个节点由数据和到序列中下一个节点的引用(换句话说,链接)组成。这种结构允许在迭代期间有效地从序列中的任何位置插入或删除元素。 |
| 6 | + |
| 7 | +更复杂的变体添加额外的链接,允许有效地插入或删除任意元素引用。链表的一个缺点是访问时间是线性的(而且难以管道化)。 |
| 8 | + |
| 9 | +更快的访问,如随机访问,是不可行的。与链表相比,数组具有更好的缓存位置。 |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +## 基本操作的伪代码 |
| 14 | + |
| 15 | +### 插入 |
| 16 | + |
| 17 | +```text |
| 18 | +Add(value) |
| 19 | + Pre: value is the value to add to the list |
| 20 | + Post: value has been placed at the tail of the list |
| 21 | + n ← node(value) |
| 22 | + if head = ø |
| 23 | + head ← n |
| 24 | + tail ← n |
| 25 | + else |
| 26 | + tail.next ← n |
| 27 | + tail ← n |
| 28 | + end if |
| 29 | +end Add |
| 30 | +``` |
| 31 | + |
| 32 | +### 搜索 |
| 33 | + |
| 34 | +```text |
| 35 | +Contains(head, value) |
| 36 | + Pre: head is the head node in the list |
| 37 | + value is the value to search for |
| 38 | + Post: the item is either in the linked list, true; otherwise false |
| 39 | + n ← head |
| 40 | + while n != ø and n.value != value |
| 41 | + n ← n.next |
| 42 | + end while |
| 43 | + if n = ø |
| 44 | + return false |
| 45 | + end if |
| 46 | + return true |
| 47 | +end Contains |
| 48 | +``` |
| 49 | + |
| 50 | +### 删除 |
| 51 | + |
| 52 | +```text |
| 53 | +Remove(head, value) |
| 54 | + Pre: head is the head node in the list |
| 55 | + value is the value to remove from the list |
| 56 | + Post: value is removed from the list, true, otherwise false |
| 57 | + if head = ø |
| 58 | + return false |
| 59 | + end if |
| 60 | + n ← head |
| 61 | + if n.value = value |
| 62 | + if head = tail |
| 63 | + head ← ø |
| 64 | + tail ← ø |
| 65 | + else |
| 66 | + head ← head.next |
| 67 | + end if |
| 68 | + return true |
| 69 | + end if |
| 70 | + while n.next = ø and n.next.value = value |
| 71 | + n ← n.next |
| 72 | + end while |
| 73 | + if n.next = ø |
| 74 | + if n.next = tail |
| 75 | + tail ← n |
| 76 | + end if |
| 77 | + n.next ← n.next.next |
| 78 | + return true |
| 79 | + end if |
| 80 | + return false |
| 81 | +end Remove |
| 82 | +``` |
| 83 | + |
| 84 | +### 遍历 |
| 85 | + |
| 86 | +```text |
| 87 | +Traverse(head) |
| 88 | + Pre: head is the head node in the list |
| 89 | + Post: the items in the list have been traversed |
| 90 | + n ← head |
| 91 | + while n = 0 |
| 92 | + yield n.value |
| 93 | + n ← n.next |
| 94 | + end while |
| 95 | +end Traverse |
| 96 | +``` |
| 97 | + |
| 98 | +### 反向遍历 |
| 99 | + |
| 100 | +```text |
| 101 | +ReverseTraversal(head, tail) |
| 102 | + Pre: head and tail belong to the same list |
| 103 | + Post: the items in the list have been traversed in reverse order |
| 104 | + if tail = ø |
| 105 | + curr ← tail |
| 106 | + while curr = head |
| 107 | + prev ← head |
| 108 | + while prev.next = curr |
| 109 | + prev ← prev.next |
| 110 | + end while |
| 111 | + yield curr.value |
| 112 | + curr ← prev |
| 113 | + end while |
| 114 | + yeild curr.value |
| 115 | + end if |
| 116 | +end ReverseTraversal |
| 117 | +``` |
| 118 | + |
| 119 | +## 复杂度 |
| 120 | + |
| 121 | +### 时间复杂度 |
| 122 | + |
| 123 | +| Access | Search | Insertion | Deletion | |
| 124 | +| :-------: | :-------: | :-------: | :-------: | |
| 125 | +| O(n) | O(n) | O(1) | O(1) | |
| 126 | + |
| 127 | +### 空间复杂度 |
| 128 | + |
| 129 | +O(n) |
| 130 | + |
| 131 | +## 参考 |
| 132 | + |
| 133 | +- [Wikipedia](https://en.wikipedia.org/wiki/Linked_list) |
| 134 | +- [YouTube](https://www.youtube.com/watch?v=njTh_OwMljA&index=2&t=1s&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8) |
0 commit comments