@@ -73,7 +73,8 @@ public final class LinkedList<T> {
73
73
self . append ( newNode)
74
74
}
75
75
76
- public func append( _ newNode: Node ) {
76
+ public func append( _ node: Node ) {
77
+ let newNode = LinkedListNode ( value: node. value)
77
78
if let lastNode = last {
78
79
newNode. previous = lastNode
79
80
lastNode. next = newNode
@@ -104,9 +105,9 @@ public final class LinkedList<T> {
104
105
self . insert ( newNode, atIndex: index)
105
106
}
106
107
107
- public func insert( _ newNode : Node , atIndex index: Int ) {
108
+ public func insert( _ node : Node , atIndex index: Int ) {
108
109
let ( prev, next) = nodesBeforeAndAfter ( index: index)
109
-
110
+ let newNode = LinkedListNode ( value : node . value )
110
111
newNode. previous = prev
111
112
newNode. next = next
112
113
prev? . next = newNode
@@ -264,24 +265,24 @@ f // [Universe, Swifty]
264
265
//list.removeAll()
265
266
//list.isEmpty
266
267
267
- list. remove ( node: list. first!) // "Hello "
268
+ list. remove ( node: list. first!) // "Universe "
268
269
list. count // 2
269
- list [ 0 ] // "Swift "
270
- list [ 1 ] // "World "
270
+ list [ 0 ] // "Swifty "
271
+ list [ 1 ] // "Hello "
271
272
272
- list. removeLast ( ) // "World "
273
+ list. removeLast ( ) // "Hello "
273
274
list. count // 1
274
- list [ 0 ] // "Swift "
275
+ list [ 0 ] // "Swifty "
275
276
276
- list. remove ( atIndex: 0 ) // "Swift "
277
+ list. remove ( atIndex: 0 ) // "Swifty "
277
278
list. count // 0
278
279
279
280
let linkedList : LinkedList < Int > = [ 1 , 2 , 3 , 4 ] // [1, 2, 3, 4]
280
281
linkedList. count // 4
281
282
linkedList [ 0 ] // 1
282
283
283
284
// Infer the type from the array
284
- let listArrayLiteral2 : LinkedList = [ " Swift " , " Algorithm " , " Club " ]
285
- listArrayLiteral2. count // 3
286
- listArrayLiteral2 [ 0 ] // "Swift"
287
- listArrayLiteral2. removeLast ( ) // "Club"
285
+ let listArrayLiteral2 : LinkedList ? = [ " Swift " , " Algorithm " , " Club " ]
286
+ listArrayLiteral2? . count // 3
287
+ listArrayLiteral2 ? [ 0 ] // "Swift"
288
+ listArrayLiteral2? . removeLast ( ) // "Club"
0 commit comments