You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Linked List/README.markdown
+13-13Lines changed: 13 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -216,7 +216,7 @@ It loops through the list in the same manner but this time increments a counter
216
216
What if we wanted to find the node at a specific index in the list? With an array we can just write `array[index]` and it's an **O(1)** operation. It's a bit more involved with linked lists, but again the code follows a similar pattern:
217
217
218
218
```swift
219
-
publicfuncnodeAtIndex(index: Int) -> Node? {
219
+
publicfuncnodeAt(_index: Int) -> Node? {
220
220
if index >=0 {
221
221
var node = head
222
222
var i = index
@@ -235,16 +235,16 @@ The loop looks a little different but it does the same thing: it starts at `head
235
235
Try it out:
236
236
237
237
```swift
238
-
list.nodeAtIndex(0)!.value// "Hello"
239
-
list.nodeAtIndex(1)!.value// "World"
240
-
list.nodeAtIndex(2) // nil
238
+
list.nodeAt(0)!.value// "Hello"
239
+
list.nodeAt(1)!.value// "World"
240
+
list.nodeAt(2) // nil
241
241
```
242
242
243
243
For fun we can implement a `subscript` method too:
244
244
245
245
```swift
246
246
publicsubscript(index: Int) -> T {
247
-
let node =nodeAtIndex(index)
247
+
let node =nodeAt(index)
248
248
assert(node !=nil)
249
249
return node!.value
250
250
}
@@ -367,7 +367,7 @@ If you had a tail pointer, you'd set it to `nil` here too.
367
367
Next we'll add some functions that let you remove individual nodes. If you already have a reference to the node, then using `removeNode()` is the most optimal because you don't need to iterate through the list to find the node first.
368
368
369
369
```swift
370
-
publicfuncremoveNode(node: Node) -> T {
370
+
publicfuncremove(node: Node) -> T {
371
371
let prev = node.previous
372
372
let next = node.next
373
373
@@ -391,24 +391,24 @@ Don't forget the `head` pointer! If this was the first node in the list then `he
391
391
Try it out:
392
392
393
393
```swift
394
-
list.removeNode(list.first!) // "Hello"
394
+
list.remove(list.first!) // "Hello"
395
395
list.count// 2
396
396
list[0] // "Swift"
397
397
list[1] // "World"
398
398
```
399
399
400
-
If you don't have a reference to the node, you can use `removeLast()` or `removeAtIndex()`:
400
+
If you don't have a reference to the node, you can use `removeLast()` or `removeAt()`:
0 commit comments