|
| 1 | +# 707. Design Linked List - LeetCode Solution |
| 2 | +LeetCode problem link: [707. Design Linked List](https://leetcode.com/problems/design-linked-list), |
| 3 | +[707. 设计链表](https://leetcode.cn/problems/design-linked-list) |
| 4 | + |
| 5 | +[中文题解](#中文题解) |
| 6 | + |
| 7 | +## LeetCode problem description |
| 8 | +Design your implementation of the linked list. You can choose to use a singly or doubly linked list. |
| 9 | + |
| 10 | +A node in a singly linked list should have two attributes: `val` and `next`. `val` is the value of the current node, and `next` is a pointer/reference to the next node. |
| 11 | + |
| 12 | +If you want to use the doubly linked list, you will need one more attribute `prev` to indicate the previous node in the linked list. Assume all nodes in the linked list are **0-indexed**. |
| 13 | + |
| 14 | + |
| 15 | +Implement the `MyLinkedList` class: |
| 16 | + |
| 17 | +* `MyLinkedList()` Initializes the `MyLinkedList` object. |
| 18 | +* `int get(int index)` Get the value of the `index-th` node in the linked list. If the index is invalid, return `-1`. |
| 19 | +* `void addAtHead(int val)` Add a node of value `val` before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. |
| 20 | +* `void addAtTail(int val)` Append a node of value `val` as the last element of the linked list. |
| 21 | +* `void addAtIndex(int index, int val)` Add a node of value `val` before the `index-th` node in the linked list. If `index` equals the length of the linked list, the node will be appended to the end of the linked list. If `index` is greater than the length, the node will **not be inserted**. |
| 22 | +* `void deleteAtIndex(int index)` Delete the `index-th` node in the linked list, if the index is valid. |
| 23 | + |
| 24 | +### [Example 1] |
| 25 | +**Input** |
| 26 | +```ruby |
| 27 | +["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"] |
| 28 | +[[], [1], [3], [1, 2], [1], [1], [1]] |
| 29 | +``` |
| 30 | + |
| 31 | +**Output** |
| 32 | +```ruby |
| 33 | +[null, null, null, null, 2, null, 3] |
| 34 | +``` |
| 35 | + |
| 36 | +**Explanation** |
| 37 | +```java |
| 38 | +MyLinkedList myLinkedList = new MyLinkedList(); |
| 39 | +myLinkedList.addAtHead(1); |
| 40 | +myLinkedList.addAtTail(3); |
| 41 | +myLinkedList.addAtIndex(1, 2); // linked list becomes 1->2->3 |
| 42 | +myLinkedList.get(1); // return 2 |
| 43 | +myLinkedList.deleteAtIndex(1); // now the linked list is 1->3 |
| 44 | +myLinkedList.get(1); // return 3 |
| 45 | +``` |
| 46 | + |
| 47 | +### [Constraints] |
| 48 | +- `0 <= index, val <= 1000` |
| 49 | +- Please do not use the built-in LinkedList library. |
| 50 | +- At most `2000` calls will be made to `get`, `addAtHead`, `addAtTail`, `addAtIndex` and `deleteAtIndex`. |
| 51 | + |
| 52 | +## Intuition behind the Solution |
| 53 | + |
| 54 | +## Steps to the Solution |
| 55 | + |
| 56 | + |
| 57 | +## Complexity |
| 58 | +* Time: `O()`. |
| 59 | +* Space: `O()`. |
| 60 | + |
| 61 | +## Java |
| 62 | +```java |
| 63 | +class LinkedNode { |
| 64 | + int val; |
| 65 | + LinkedNode next; |
| 66 | + |
| 67 | + LinkedNode(int val) { |
| 68 | + this.val = val; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +class MyLinkedList { |
| 73 | + private LinkedNode dummyHead = new LinkedNode(0); |
| 74 | + |
| 75 | + public MyLinkedList() {} |
| 76 | + |
| 77 | + public int get(int index) { |
| 78 | + var node = dummyHead.next; |
| 79 | + var i = 0; |
| 80 | + |
| 81 | + while (node != null && i < index) { |
| 82 | + node = node.next; |
| 83 | + i += 1; |
| 84 | + } |
| 85 | + |
| 86 | + if (i == index && node != null) { |
| 87 | + return node.val; |
| 88 | + } |
| 89 | + |
| 90 | + return -1; |
| 91 | + } |
| 92 | + |
| 93 | + public void addAtHead(int val) { |
| 94 | + var node = new LinkedNode(val); |
| 95 | + node.next = dummyHead.next; |
| 96 | + dummyHead.next = node; |
| 97 | + } |
| 98 | + |
| 99 | + public void addAtTail(int val) { |
| 100 | + var node = dummyHead; |
| 101 | + |
| 102 | + while (node.next != null) { |
| 103 | + node = node.next; |
| 104 | + } |
| 105 | + |
| 106 | + node.next = new LinkedNode(val); |
| 107 | + } |
| 108 | + |
| 109 | + public void addAtIndex(int index, int val) { |
| 110 | + var node = dummyHead; |
| 111 | + var i = 0; |
| 112 | + |
| 113 | + while (node.next != null && i < index) { |
| 114 | + node = node.next; |
| 115 | + i += 1; |
| 116 | + } |
| 117 | + |
| 118 | + if (i == index) { |
| 119 | + var newNode = new LinkedNode(val); |
| 120 | + newNode.next = node.next; |
| 121 | + node.next = newNode; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + public void deleteAtIndex(int index) { |
| 126 | + var node = dummyHead; |
| 127 | + var i = 0; |
| 128 | + |
| 129 | + while (node.next != null && i < index) { |
| 130 | + node = node.next; |
| 131 | + i += 1; |
| 132 | + } |
| 133 | + |
| 134 | + if (i == index && node.next != null) { |
| 135 | + node.next = node.next.next; |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +## Python |
| 142 | +```python |
| 143 | +class LinkedNode: |
| 144 | + def __init__(self, val=None): |
| 145 | + self.val = val |
| 146 | + self.next = None |
| 147 | + |
| 148 | + |
| 149 | +class MyLinkedList: |
| 150 | + def __init__(self): |
| 151 | + self.dummy_head = LinkedNode() |
| 152 | + |
| 153 | + def get(self, index: int) -> int: |
| 154 | + node = self.dummy_head.next |
| 155 | + i = 0 |
| 156 | + |
| 157 | + while node and i < index: |
| 158 | + node = node.next |
| 159 | + i += 1 |
| 160 | + |
| 161 | + if i == index and node: |
| 162 | + return node.val |
| 163 | + |
| 164 | + return -1 |
| 165 | + |
| 166 | + def addAtHead(self, val: int) -> None: |
| 167 | + node = LinkedNode(val) |
| 168 | + node.next = self.dummy_head.next |
| 169 | + self.dummy_head.next = node |
| 170 | + |
| 171 | + def addAtTail(self, val: int) -> None: |
| 172 | + node = self.dummy_head |
| 173 | + |
| 174 | + while node.next: |
| 175 | + node = node.next |
| 176 | + |
| 177 | + node.next = LinkedNode(val) |
| 178 | + |
| 179 | + def addAtIndex(self, index: int, val: int) -> None: |
| 180 | + node = self.dummy_head |
| 181 | + i = 0 |
| 182 | + |
| 183 | + while node.next and i < index: |
| 184 | + node = node.next |
| 185 | + i += 1 |
| 186 | + |
| 187 | + if i == index: |
| 188 | + new_node = LinkedNode(val) |
| 189 | + new_node.next = node.next |
| 190 | + node.next = new_node |
| 191 | + |
| 192 | + def deleteAtIndex(self, index: int) -> None: |
| 193 | + node = self.dummy_head |
| 194 | + i = 0 |
| 195 | + |
| 196 | + while node.next and i < index: |
| 197 | + node = node.next |
| 198 | + i += 1 |
| 199 | + |
| 200 | + if i == index and node.next: |
| 201 | + node.next = node.next.next |
| 202 | +``` |
| 203 | + |
| 204 | +## C++ |
| 205 | +```cpp |
| 206 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 207 | +``` |
| 208 | + |
| 209 | +## JavaScript |
| 210 | +```javascript |
| 211 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 212 | +``` |
| 213 | + |
| 214 | +## C# |
| 215 | +```c# |
| 216 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 217 | +``` |
| 218 | + |
| 219 | +## Go |
| 220 | +```go |
| 221 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 222 | +``` |
| 223 | + |
| 224 | +## Ruby |
| 225 | +```ruby |
| 226 | +# Welcome to create a PR to complete the code of this language, thanks! |
| 227 | +``` |
| 228 | + |
| 229 | +## C |
| 230 | +```c |
| 231 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 232 | +``` |
| 233 | + |
| 234 | +## Kotlin |
| 235 | +```kotlin |
| 236 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 237 | +``` |
| 238 | + |
| 239 | +## Swift |
| 240 | +```swift |
| 241 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 242 | +``` |
| 243 | + |
| 244 | +## Rust |
| 245 | +```rust |
| 246 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 247 | +``` |
| 248 | + |
| 249 | +## Other languages |
| 250 | +``` |
| 251 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 252 | +``` |
| 253 | + |
| 254 | +## 问题描述 |
| 255 | + |
| 256 | + |
| 257 | +### [Example 1] |
| 258 | + |
| 259 | + |
| 260 | +**Input**: `` |
| 261 | + |
| 262 | +**Output**: `` |
| 263 | + |
| 264 | +## 中文题解 |
| 265 | + |
0 commit comments