# Errata for Data Structures & Algorithms in Swift, 5th Edition

**URL:** <https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-swift-5th-edition/209429>\
**Category:** Data Structures & Algorithms in Swift\
**Created:** [January 22, 2025, 1:00am UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-swift-5th-edition/209429 "2025-01-22T01:00:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jellodiil](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jellodiil/32/109742_2.png) [@jellodiil](https://forums.kodeco.com/u/jellodiil)\
**Post date:** [January 22, 2025, 1:00am UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-swift-5th-edition/209429/1 "2025-01-22T01:00:29Z")

</div>

Creating this topic to catch any typos and bugs in the 5th Edition of Data Structures & Algorithms in Swift.

---

<div class="post-metadata">

**Author:** ![elichigo074](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/elichigo074/32/110937_2.png) [@elichigo074](https://forums.kodeco.com/u/elichigo074)\
**Post date:** [January 28, 2025, 10:20pm UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-swift-5th-edition/209429/3 "2025-01-28T22:20:27Z")

</div>

Hi @jellodiil

I found a potential mistake in one of the challenges, but I’m not sure if I might have missed something from the lesson. In [**Graph Chapter → DFS lesson → Challenge 3 (Checking for a Cycle in a Graph)**](https://www.kodeco.com/books/data-structures-algorithms-in-swift/v5.0/chapters/41-depth-first-search-challenges), the provided solution seems to always return true, even when the graph does not contain a cycle.

let graph = AdjacencyList()  
let a = graph.createVertex(data: “A”)  
let b = graph.createVertex(data: “B”)  
let c = graph.createVertex(data: “C”)  
let d = graph.createVertex(data: “D”)  
graph.add(.directed, from: a, to: b, weight: nil)  
graph.add(.directed, from: b, to: c, weight: nil)  
graph.add(.directed, from: c, to: a, weight: nil)  
graph.add(.directed, from: c, to: d, weight: nil)  
[//graph.add](https://graph.add)(.directed, from: d, to: c, weight: nil) // This will cause a cycle

print(“Has cycle: (graph.hasCycle(from: a))”)

Based on this setup, the graph should not contain a cycle unless we explicitly add the edge from d to c. However, graph.hasCycle(from: a) always returns true, which seems incorrect.

Since this is my first time posting here, I’d appreciate any guidance on whether I should report this differently. Let me know if you need more details!

---

<div class="post-metadata">

**Author:** ![jecht83](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jecht83/32/111154_2.png) [@jecht83](https://forums.kodeco.com/u/jecht83)\
**Post date:** [February 17, 2025, 6:31pm UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-swift-5th-edition/209429/6 "2025-02-17T18:31:54Z")

</div>

is this book going to be available as an update for previous purchases?

right now is listed as a 5th edition but only available to download until 4th edition

---

<div class="post-metadata">

**Author:** ![exchessplayerh](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/exchessplayerh/32/110937_2.png) [@exchessplayerh](https://forums.kodeco.com/u/exchessplayerh)\
**Post date:** [July 3, 2025, 11:41am UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-swift-5th-edition/209429/7 "2025-07-03T11:41:31Z")

</div>

There is an error in LinkedList index implementation.

```auto
extension LinkedList: Collection {

  public struct Index: Comparable {

    public var node: Node<Value>?

    static public func ==(lhs: Index, rhs: Index) -> Bool {
      switch (lhs.node, rhs.node) {
      case let (left?, right?):
        return left.next === right.next
      case (nil, nil):
        return true
      default:
        return false
      }
    }

    static public func <(lhs: Index, rhs: Index) -> Bool {
      guard lhs != rhs else {
        return false
      }
      let nodes = sequence(first: lhs.node) { $0?.next }
      return nodes.contains { $0 === rhs.node }
    }
  }
}

```

The comparison should just compare the nodes for referential equality, as it is both simpler, and it is possible that two nodes have same next node, while not being equal (hence difference positions, and probably in different lists…).

---

<div class="post-metadata">

**Author:** ![exchessplayerh](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/exchessplayerh/32/110937_2.png) [@exchessplayerh](https://forums.kodeco.com/u/exchessplayerh)\
**Post date:** [July 9, 2025, 12:36am UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-swift-5th-edition/209429/8 "2025-07-09T00:36:22Z")

</div>

In Chapter 7 Solutions, the “naive” solution to exercise 3 is wrong, as only the head gets updated, while tail remains invalid.

---

<div class="post-metadata">

**Author:** ![exchessplayerh](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/exchessplayerh/32/110937_2.png) [@exchessplayerh](https://forums.kodeco.com/u/exchessplayerh)\
**Post date:** [July 9, 2025, 2:55am UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-swift-5th-edition/209429/9 "2025-07-09T02:55:13Z")

</div>

The solution to Chapter 7, Exercise 4 is a bit problematic as well. The result breaks the lists, as it modifies their nodes directly instead of copying them. I understand that it is done to illustrate working with linked structures, but such functions should in general be made `inout`, and since it is a beginner book after all, it I believe it should be emphasized that direct access to mutable nodes is a bad practice (they should not be exposed, at least the mutable parts), as it allows to break the LinkledList value semantics by explicitly manipulating its nodes.  
Anyway, the solution should be accept two `inout` lists, as it modifies them.

---

<div class="post-metadata">

**Author:** ![exchessplayerh](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/exchessplayerh/32/110937_2.png) [@exchessplayerh](https://forums.kodeco.com/u/exchessplayerh)\
**Post date:** [July 9, 2025, 6:11am UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-swift-5th-edition/209429/10 "2025-07-09T06:11:43Z")

</div>

Another option would to make them `consuming` with appropriate adjustments.

---

<div class="post-metadata">

**Author:** ![exchessplayerh](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/exchessplayerh/32/110937_2.png) [@exchessplayerh](https://forums.kodeco.com/u/exchessplayerh)\
**Post date:** [July 9, 2025, 6:45am UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-swift-5th-edition/209429/11 "2025-07-09T06:45:12Z")

</div>

I believe semething like this should do the job.  
Another option would to make them `consuming` with appropriate adjustments.

I believe something like this guarantees that copy is performed, unless the the lists are unique (that’s why we use consuming) in which case nothing is copied.

```swift
func mergeSorted<T: Comparable>(
_ left: consuming LinkedList<T>, 
with right: consuming LinkedList<T>
) -> LinkedList<T> {
        var currentLeftNode = left.copyNodes(returningCopyOf: left.head)
        var currentRightNode = right.copyNodes(returningCopyOf: right.head)

        let newHead: Node<T>?

        switch (currentLeftNode?.value, currentRightNode?.value) {
        case (nil, nil):
            return left
        case (nil, _):
            return right
        case (_, nil):
            return left
        case let (leftValue?, rightValue?) where leftValue < rightValue:
            newHead = currentLeftNode
            currentLeftNode = currentLeftNode?.next
        default:
            newHead = currentRightNode
            currentRightNode = currentRightNode?.next
        }

        var currentTail = newHead

        while currentLeftNode != nil && currentRightNode != nil {
            switch (currentLeftNode?.value, currentRightNode?.value) {
            case (let leftValue?, let rightValue?) where leftValue < rightValue:
                currentTail?.next = currentLeftNode
                currentLeftNode = currentLeftNode?.next
            default:
                currentTail?.next = currentRightNode
                currentRightNode = currentRightNode?.next
            }
            currentTail = currentTail?.next
        }

        currentTail?.next = currentLeftNode ?? currentRightNode
        currentTail = if currentLeftNode != nil { left.tail } else { right.tail } 

        var result: LinkedList = .init()
        result.head = newHead
        result.tail = currentTail

        return result
}
```
