Skip to content

Commit c15c11b

Browse files
authored
Merge pull request kodecocodes#429 from m-alani/master
Fixing a bug in the Linked List logic
2 parents 210f4f4 + 3028c65 commit c15c11b

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

Linked List/LinkedList.playground/Contents.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ public final class LinkedList<T> {
7373
self.append(newNode)
7474
}
7575

76-
public func append(_ newNode: Node) {
76+
public func append(_ node: Node) {
77+
let newNode = LinkedListNode(value: node.value)
7778
if let lastNode = last {
7879
newNode.previous = lastNode
7980
lastNode.next = newNode
@@ -104,9 +105,9 @@ public final class LinkedList<T> {
104105
self.insert(newNode, atIndex: index)
105106
}
106107

107-
public func insert(_ newNode: Node, atIndex index: Int) {
108+
public func insert(_ node: Node, atIndex index: Int) {
108109
let (prev, next) = nodesBeforeAndAfter(index: index)
109-
110+
let newNode = LinkedListNode(value: node.value)
110111
newNode.previous = prev
111112
newNode.next = next
112113
prev?.next = newNode
@@ -264,16 +265,16 @@ f // [Universe, Swifty]
264265
//list.removeAll()
265266
//list.isEmpty
266267

267-
list.remove(node: list.first!) // "Hello"
268+
list.remove(node: list.first!) // "Universe"
268269
list.count // 2
269-
list[0] // "Swift"
270-
list[1] // "World"
270+
list[0] // "Swifty"
271+
list[1] // "Hello"
271272

272-
list.removeLast() // "World"
273+
list.removeLast() // "Hello"
273274
list.count // 1
274-
list[0] // "Swift"
275+
list[0] // "Swifty"
275276

276-
list.remove(atIndex: 0) // "Swift"
277+
list.remove(atIndex: 0) // "Swifty"
277278
list.count // 0
278279

279280
let linkedList: LinkedList<Int> = [1, 2, 3, 4] // [1, 2, 3, 4]
@@ -284,4 +285,4 @@ linkedList[0] // 1
284285
let listArrayLiteral2: LinkedList = ["Swift", "Algorithm", "Club"]
285286
listArrayLiteral2.count // 3
286287
listArrayLiteral2[0] // "Swift"
287-
listArrayLiteral2.removeLast() // "Club"
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)