From 4f6c4bc5788c1e55f09fad659e1e1037111de302 Mon Sep 17 00:00:00 2001 From: Avi Levin Date: Fri, 26 Feb 2021 22:53:49 +0200 Subject: [PATCH 1/2] fix(link-list): remove append of node by node in list appending method, we can append the newLinkedList head to the oldLinkedList last(tail) item. --- .../LinkedList.playground/Contents.swift | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/Linked List/LinkedList.playground/Contents.swift b/Linked List/LinkedList.playground/Contents.swift index 76b911310..8f9c5b779 100644 --- a/Linked List/LinkedList.playground/Contents.swift +++ b/Linked List/LinkedList.playground/Contents.swift @@ -81,12 +81,9 @@ public final class LinkedList { var node = head!.next for _ in 1.. { /// /// - Parameter list: The list to be copied and appended. public func append(_ list: LinkedList) { - var nodeToCopy = list.head - while let node = nodeToCopy { - append(node.value) - nodeToCopy = node.next + + guard let headNode = list.head else { + return } + + append(headNode) } /// Insert a value at a specific index. Crashes if index is out of bounds (0...self.count) @@ -380,13 +378,18 @@ list2.append("World") list.append(list2) // [Hello, World, Goodbye, World] list2.removeAll() // [ ] list2.isEmpty // true + + +print(list) list.removeLast() // "World" +list list.remove(at: 2) // "Goodbye" list.insert("Swift", at: 1) list[0] // "Hello" list[1] // "Swift" list[2] // "World" +//list[10] // crash!! print(list) list.reverse() // [World, Swift, Hello] From 1345fd2d479ccdb81ddebe1abc33b7df1efb509e Mon Sep 17 00:00:00 2001 From: Avi Levin Date: Fri, 26 Feb 2021 23:06:45 +0200 Subject: [PATCH 2/2] refac(Linked List): remove unnecessary prints --- Linked List/LinkedList.playground/Contents.swift | 3 --- 1 file changed, 3 deletions(-) diff --git a/Linked List/LinkedList.playground/Contents.swift b/Linked List/LinkedList.playground/Contents.swift index 8f9c5b779..70e8319ac 100644 --- a/Linked List/LinkedList.playground/Contents.swift +++ b/Linked List/LinkedList.playground/Contents.swift @@ -379,10 +379,7 @@ list.append(list2) // [Hello, World, Goodbye, World] list2.removeAll() // [ ] list2.isEmpty // true - -print(list) list.removeLast() // "World" -list list.remove(at: 2) // "Goodbye" list.insert("Swift", at: 1)