Skip to content

Commit 9857da6

Browse files
committed
Updated README.md
1 parent b0baae9 commit 9857da6

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

Linked List/README.markdown

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ It loops through the list in the same manner but this time increments a counter
216216
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:
217217

218218
```swift
219-
public func nodeAtIndex(index: Int) -> Node? {
219+
public func nodeAt(_ index: Int) -> Node? {
220220
if index >= 0 {
221221
var node = head
222222
var i = index
@@ -235,16 +235,16 @@ The loop looks a little different but it does the same thing: it starts at `head
235235
Try it out:
236236

237237
```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
241241
```
242242

243243
For fun we can implement a `subscript` method too:
244244

245245
```swift
246246
public subscript(index: Int) -> T {
247-
let node = nodeAtIndex(index)
247+
let node = nodeAt(index)
248248
assert(node != nil)
249249
return node!.value
250250
}
@@ -367,7 +367,7 @@ If you had a tail pointer, you'd set it to `nil` here too.
367367
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.
368368

369369
```swift
370-
public func removeNode(node: Node) -> T {
370+
public func remove(node: Node) -> T {
371371
let prev = node.previous
372372
let next = node.next
373373

@@ -391,24 +391,24 @@ Don't forget the `head` pointer! If this was the first node in the list then `he
391391
Try it out:
392392

393393
```swift
394-
list.removeNode(list.first!) // "Hello"
394+
list.remove(list.first!) // "Hello"
395395
list.count // 2
396396
list[0] // "Swift"
397397
list[1] // "World"
398398
```
399399

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()`:
401401

402402
```swift
403403
public func removeLast() -> T {
404404
assert(!isEmpty)
405-
return removeNode(last!)
405+
return remove(node: last!)
406406
}
407407

408-
public func removeAtIndex(index: Int) -> T {
409-
let node = nodeAtIndex(index)
408+
public func removeAt(_ index: Int) -> T {
409+
let node = nodeAt(index)
410410
assert(node != nil)
411-
return removeNode(node!)
411+
return remove(node: node!)
412412
}
413413
```
414414

@@ -419,7 +419,7 @@ list.removeLast() // "World"
419419
list.count // 1
420420
list[0] // "Swift"
421421

422-
list.removeAtIndex(0) // "Swift"
422+
list.removeAt(0) // "Swift"
423423
list.count // 0
424424
```
425425

0 commit comments

Comments
 (0)