Skip to content

Commit 3028c65

Browse files
committed
Corrected comments in playground. Fixed a logic bug when inserting/appending a node
1 parent 93cbde7 commit 3028c65

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

Linked List/LinkedList.playground/Contents.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ linkedList.count // 4
282282
linkedList[0] // 1
283283

284284
// Infer the type from the array
285-
let listArrayLiteral2: LinkedList? = ["Swift", "Algorithm", "Club"]
286-
listArrayLiteral2?.count // 3
287-
listArrayLiteral2?[0] // "Swift"
288-
listArrayLiteral2?.removeLast() // "Club"
285+
let listArrayLiteral2: LinkedList = ["Swift", "Algorithm", "Club"]
286+
listArrayLiteral2.count // 3
287+
listArrayLiteral2[0] // "Swift"
288+
listArrayLiteral2.removeLast() // "Club"

Linked List/LinkedList.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public final class LinkedList<T> {
7171
self.append(newNode)
7272
}
7373

74-
public func append(_ newNode: Node) {
74+
public func append(_ node: Node) {
75+
let newNode = Node(value: node.value)
7576
if let lastNode = last {
7677
newNode.previous = lastNode
7778
lastNode.next = newNode
@@ -102,9 +103,9 @@ public final class LinkedList<T> {
102103
self.insert(newNode, atIndex: index)
103104
}
104105

105-
public func insert(_ newNode: Node, atIndex index: Int) {
106+
public func insert(_ node: Node, atIndex index: Int) {
106107
let (prev, next) = nodesBeforeAndAfter(index: index)
107-
108+
let newNode = Node(value: node.value)
108109
newNode.previous = prev
109110
newNode.next = next
110111
prev?.next = newNode

0 commit comments

Comments
 (0)