From 91dc8bd03439438fb6b600fd8392143672bccb5c Mon Sep 17 00:00:00 2001 From: Aaron <7063649+ahcode0919@users.noreply.github.com> Date: Sun, 16 Aug 2026 13:17:06 -0700 Subject: [PATCH 1/4] Add ReverseList --- Package.swift | 8 +++- Sources/swift-ds-algorithms/Helpers.swift | 44 +++++++++++++++++- .../LinkedList/ReverseLinkedList.swift | 35 ++++++++++++++ .../ReversseLinkedListTests.swift | 46 +++++++++++++++++++ .../{Stack => StackTests}/BracketsTests.swift | 0 .../ReverseSomeCharsTests.swift | 0 6 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 Sources/swift-ds-algorithms/LinkedList/ReverseLinkedList.swift create mode 100644 Tests/swift-ds-algorithms-tests/LinkedListTests/ReversseLinkedListTests.swift rename Tests/swift-ds-algorithms-tests/{Stack => StackTests}/BracketsTests.swift (100%) rename Tests/swift-ds-algorithms-tests/{Stack => StackTests}/ReverseSomeCharsTests.swift (100%) diff --git a/Package.swift b/Package.swift index 797d7ac..e3ea64d 100644 --- a/Package.swift +++ b/Package.swift @@ -23,9 +23,15 @@ let package = Package( targets: [ .target( name: "swift-ds-algorithms", + exclude: [ + "Arrays/README.md", + "DataStructures/README.md", + "Integers/README.md", + "Strings/README.md" + ], plugins: skipPlugins ? [] : [ .plugin(name: "SwiftLintBuildToolPlugin", package: "SwiftLintPlugins") - ], + ] ), .testTarget( name: "swift-ds-algorithms-tests", diff --git a/Sources/swift-ds-algorithms/Helpers.swift b/Sources/swift-ds-algorithms/Helpers.swift index db55cfa..7ba4363 100644 --- a/Sources/swift-ds-algorithms/Helpers.swift +++ b/Sources/swift-ds-algorithms/Helpers.swift @@ -9,6 +9,17 @@ import Foundation import GameKit public class Helpers { + public enum HelperError: Error, CustomStringConvertible { + case invalidInput(message: String) + + public var description: String { + switch self { + case .invalidInput(message: let message): + return message + } + } + } + public static func generateRandomString(ofLength length: Int = 1000000) -> String { let characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 " let charCount = characters.count @@ -17,9 +28,40 @@ public class Helpers { for index in 0.. SinglyLinkedListNode { + guard length > 0 else { + throw HelperError.invalidInput(message: "Cannot generate a linked list with a length of 0 or less") + } + let dummyHead = SinglyLinkedListNode(value: 0) + var last = dummyHead + + for index in 0..(head: SinglyLinkedListNode?) -> [T] { + var result: [T] = [] + var current = head + + while let node = current { + result.append(node.value) + current = node.next + } + return result + } } diff --git a/Sources/swift-ds-algorithms/LinkedList/ReverseLinkedList.swift b/Sources/swift-ds-algorithms/LinkedList/ReverseLinkedList.swift new file mode 100644 index 0000000..c197bcc --- /dev/null +++ b/Sources/swift-ds-algorithms/LinkedList/ReverseLinkedList.swift @@ -0,0 +1,35 @@ +// ReverseLinkedList.swift +// swift-ds-algorithms +// +// Created by Aaron on 8/16/26. +// Copyright © 2026. All rights reserved. +// + +public enum ReverseLinkedList { + public static func reverse(head: SinglyLinkedListNode?) -> SinglyLinkedListNode? { + var prev: SinglyLinkedListNode? + var current: SinglyLinkedListNode? = head + + while current != nil { + let temp = current?.next + current?.next = prev + prev = current + current = temp + } + + return prev + } + + public static func reverseRecursive( + head: SinglyLinkedListNode?, + prev: SinglyLinkedListNode? = nil + ) -> SinglyLinkedListNode? { + if head == nil { + return prev + } + + let temp = head?.next + head?.next = prev + return reverseRecursive(head: temp, prev: head) + } +} diff --git a/Tests/swift-ds-algorithms-tests/LinkedListTests/ReversseLinkedListTests.swift b/Tests/swift-ds-algorithms-tests/LinkedListTests/ReversseLinkedListTests.swift new file mode 100644 index 0000000..8219ccc --- /dev/null +++ b/Tests/swift-ds-algorithms-tests/LinkedListTests/ReversseLinkedListTests.swift @@ -0,0 +1,46 @@ +// Test.swift +// swift-ds-algorithms +// +// Created by Aaron on 8/16/26. +// Copyright © 2026. All rights reserved. +// + +import swift_ds_algorithms +import Testing + +struct ReverseLinkedListTest { + + @Test func testReverseLinkedList() throws { + var head: SinglyLinkedListNode? + #expect(ReverseLinkedList.reverse(head: head) == nil) + + head = SinglyLinkedListNode(value: 0) + var result = ReverseLinkedList.reverse(head: head) + #expect(result?.value == 0) + #expect(result?.next == nil) + + head = try Helpers.generateLinkedList(ofLength: 10) + result = ReverseLinkedList.reverse(head: head) + #expect(result != nil) + + let values = Helpers.linkedListToArray(head: result) + #expect(values == [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) + } + + @Test func testReverseLinkedListRecursive() throws { + var head: SinglyLinkedListNode? + #expect(ReverseLinkedList.reverseRecursive(head: head) == nil) + + head = SinglyLinkedListNode(value: 0) + var result = ReverseLinkedList.reverseRecursive(head: head) + #expect(result?.value == 0) + #expect(result?.next == nil) + + head = try Helpers.generateLinkedList(ofLength: 10) + result = ReverseLinkedList.reverseRecursive(head: head) + #expect(result != nil) + + let values = Helpers.linkedListToArray(head: result) + #expect(values == [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) + } +} diff --git a/Tests/swift-ds-algorithms-tests/Stack/BracketsTests.swift b/Tests/swift-ds-algorithms-tests/StackTests/BracketsTests.swift similarity index 100% rename from Tests/swift-ds-algorithms-tests/Stack/BracketsTests.swift rename to Tests/swift-ds-algorithms-tests/StackTests/BracketsTests.swift diff --git a/Tests/swift-ds-algorithms-tests/Stack/ReverseSomeCharsTests.swift b/Tests/swift-ds-algorithms-tests/StackTests/ReverseSomeCharsTests.swift similarity index 100% rename from Tests/swift-ds-algorithms-tests/Stack/ReverseSomeCharsTests.swift rename to Tests/swift-ds-algorithms-tests/StackTests/ReverseSomeCharsTests.swift From a4a93246cd11a6925ca6ca090cbf856fe43f1252 Mon Sep 17 00:00:00 2001 From: Aaron <7063649+ahcode0919@users.noreply.github.com> Date: Sun, 16 Aug 2026 13:22:53 -0700 Subject: [PATCH 2/4] Add build link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index eb876fa..acfbf7f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Swift Algorithms +# Swift Algorithms ![](https://github.com/ahcode0919/swift-ds-algorithms/actions/workflows/ci.yml/badge.svg?branch=main) Repository of solutions to various problems and algorithms in Swift. From c54c387b096095b44bee1cfb59cc64c55b4ac002 Mon Sep 17 00:00:00 2001 From: Aaron <7063649+ahcode0919@users.noreply.github.com> Date: Sun, 16 Aug 2026 14:59:08 -0700 Subject: [PATCH 3/4] Add Zippered Lists --- Sources/swift-ds-algorithms/Helpers.swift | 20 +++++++-- .../LinkedList/ZipperLists.swift | 38 ++++++++++++++++ .../LinkedListTests/ZipperListsTests.swift | 44 +++++++++++++++++++ 3 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 Sources/swift-ds-algorithms/LinkedList/ZipperLists.swift create mode 100644 Tests/swift-ds-algorithms-tests/LinkedListTests/ZipperListsTests.swift diff --git a/Sources/swift-ds-algorithms/Helpers.swift b/Sources/swift-ds-algorithms/Helpers.swift index 7ba4363..12a7965 100644 --- a/Sources/swift-ds-algorithms/Helpers.swift +++ b/Sources/swift-ds-algorithms/Helpers.swift @@ -35,9 +35,6 @@ public class Helpers { } public static func generateLinkedList(ofLength length: Int) throws -> SinglyLinkedListNode { - guard length > 0 else { - throw HelperError.invalidInput(message: "Cannot generate a linked list with a length of 0 or less") - } let dummyHead = SinglyLinkedListNode(value: 0) var last = dummyHead @@ -54,6 +51,23 @@ public class Helpers { return head } + public static func generateLinkedList(from array: [String]) throws -> SinglyLinkedListNode { + let dummyHead = SinglyLinkedListNode(value: "") + var last = dummyHead + + for value in array { + let node = SinglyLinkedListNode(value: value) + last.next = node + last = node + } + + guard let head = dummyHead.next else { + throw HelperError.invalidInput(message: "Failed to generate a linked list") + } + + return head + } + public static func linkedListToArray(head: SinglyLinkedListNode?) -> [T] { var result: [T] = [] var current = head diff --git a/Sources/swift-ds-algorithms/LinkedList/ZipperLists.swift b/Sources/swift-ds-algorithms/LinkedList/ZipperLists.swift new file mode 100644 index 0000000..7f3bd45 --- /dev/null +++ b/Sources/swift-ds-algorithms/LinkedList/ZipperLists.swift @@ -0,0 +1,38 @@ +// ZipperLists.swift +// swift-ds-algorithms +// +// Created by Aaron on 8/16/26. +// Copyright © 2026. All rights reserved. +// + +public enum ZipperLists { + public static func zipper( + head1: SinglyLinkedListNode?, + head2: SinglyLinkedListNode? + ) -> SinglyLinkedListNode? { + let dummyHead = SinglyLinkedListNode(value: "") + var current1: SinglyLinkedListNode? = head1 + var current2: SinglyLinkedListNode? = head2 + var current: SinglyLinkedListNode? = dummyHead + + while let node1 = current1, let node2 = current2 { + current?.next = node1 + current1 = node1.next + current = current?.next + + current?.next = node2 + current2 = node2.next + current = current?.next + } + + if let node = current1 { + current?.next = node + } + + if let node = current2 { + current?.next = node + } + + return dummyHead.next + } +} diff --git a/Tests/swift-ds-algorithms-tests/LinkedListTests/ZipperListsTests.swift b/Tests/swift-ds-algorithms-tests/LinkedListTests/ZipperListsTests.swift new file mode 100644 index 0000000..5d0240a --- /dev/null +++ b/Tests/swift-ds-algorithms-tests/LinkedListTests/ZipperListsTests.swift @@ -0,0 +1,44 @@ +// Test.swift +// swift-ds-algorithms +// +// Created by Aaron on 8/16/26. +// Copyright © 2026. All rights reserved. +// + +import swift_ds_algorithms +import Testing + +struct ZipperListsTests { + + @Test func zipperLists() throws { + var result = ZipperLists.zipper(head1: nil, head2: nil) + #expect(result == nil) + + var list1 = try Helpers.generateLinkedList(from: ["a", "b", "c"]) + var list2 = try Helpers.generateLinkedList(from: ["x", "y", "z"]) + result = ZipperLists.zipper(head1: list1, head2: list2) + + var values = Helpers.linkedListToArray(head: result) + #expect(values == ["a", "x", "b", "y", "c", "z"]) + + list1 = try Helpers.generateLinkedList(from: ["a", "b", "c"]) + list2 = try Helpers.generateLinkedList(from: ["x", "y"]) + result = ZipperLists.zipper(head1: list1, head2: list2) + + values = Helpers.linkedListToArray(head: result) + #expect(values == ["a", "x", "b", "y", "c"]) + + list1 = try Helpers.generateLinkedList(from: ["a", "b"]) + list2 = try Helpers.generateLinkedList(from: ["x", "y", "z"]) + result = ZipperLists.zipper(head1: list1, head2: list2) + + values = Helpers.linkedListToArray(head: result) + #expect(values == ["a", "x", "b", "y", "z"]) + + list1 = try Helpers.generateLinkedList(from: ["a", "b", "c"]) + result = ZipperLists.zipper(head1: list1, head2: nil) + + values = Helpers.linkedListToArray(head: result) + #expect(values == ["a", "b", "c"]) + } +} From b524bacfe9647bf196e977a9726b590109824be9 Mon Sep 17 00:00:00 2001 From: Aaron <7063649+ahcode0919@users.noreply.github.com> Date: Sun, 16 Aug 2026 15:38:59 -0700 Subject: [PATCH 4/4] Add MergeLists --- Sources/swift-ds-algorithms/Helpers.swift | 18 ++++++--- .../LinkedList/MergeLists.swift | 39 +++++++++++++++++++ .../LinkedListTests/MergeListsTests.swift | 31 +++++++++++++++ ...sts.swift => ReverseLinkedListTests.swift} | 0 4 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 Sources/swift-ds-algorithms/LinkedList/MergeLists.swift create mode 100644 Tests/swift-ds-algorithms-tests/LinkedListTests/MergeListsTests.swift rename Tests/swift-ds-algorithms-tests/LinkedListTests/{ReversseLinkedListTests.swift => ReverseLinkedListTests.swift} (100%) diff --git a/Sources/swift-ds-algorithms/Helpers.swift b/Sources/swift-ds-algorithms/Helpers.swift index 12a7965..68a7fff 100644 --- a/Sources/swift-ds-algorithms/Helpers.swift +++ b/Sources/swift-ds-algorithms/Helpers.swift @@ -51,17 +51,23 @@ public class Helpers { return head } - public static func generateLinkedList(from array: [String]) throws -> SinglyLinkedListNode { - let dummyHead = SinglyLinkedListNode(value: "") - var last = dummyHead + public static func generateLinkedList(from array: [T]) throws -> SinglyLinkedListNode { + var head: SinglyLinkedListNode? + var last: SinglyLinkedListNode? for value in array { let node = SinglyLinkedListNode(value: value) - last.next = node - last = node + + if head == nil { + head = SinglyLinkedListNode(value: value) + last = head + } else { + last?.next = node + last = node + } } - guard let head = dummyHead.next else { + guard let head else { throw HelperError.invalidInput(message: "Failed to generate a linked list") } diff --git a/Sources/swift-ds-algorithms/LinkedList/MergeLists.swift b/Sources/swift-ds-algorithms/LinkedList/MergeLists.swift new file mode 100644 index 0000000..118ba48 --- /dev/null +++ b/Sources/swift-ds-algorithms/LinkedList/MergeLists.swift @@ -0,0 +1,39 @@ +// MergeLists.swift +// swift-ds-algorithms +// +// Created by Aaron on 8/16/26. +// Copyright © 2026. All rights reserved. +// + +public enum MergeLists { + public static func merge( + head1: SinglyLinkedListNode?, + head2: SinglyLinkedListNode? + ) -> SinglyLinkedListNode? { + let dummyHead = SinglyLinkedListNode(value: 0) + var current: SinglyLinkedListNode? = dummyHead + var currentHead1 = head1 + var currentHead2 = head2 + + while let node1 = currentHead1, let node2 = currentHead2 { + if node1.value < node2.value { + current?.next = node1 + currentHead1 = node1.next + } else { + current?.next = node2 + currentHead2 = node2.next + } + current = current?.next + } + + if let node = currentHead1 { + current?.next = node + } + + if let node = currentHead2 { + current?.next = node + } + + return dummyHead.next + } +} diff --git a/Tests/swift-ds-algorithms-tests/LinkedListTests/MergeListsTests.swift b/Tests/swift-ds-algorithms-tests/LinkedListTests/MergeListsTests.swift new file mode 100644 index 0000000..4542889 --- /dev/null +++ b/Tests/swift-ds-algorithms-tests/LinkedListTests/MergeListsTests.swift @@ -0,0 +1,31 @@ +// Test.swift +// swift-ds-algorithms +// +// Created by Aaron on 8/16/26. +// Copyright © 2026. All rights reserved. +// + +import swift_ds_algorithms +import Testing + +struct MergeListsTests { + + @Test func testMergeLists() throws { + var list1: SinglyLinkedListNode? = try Helpers.generateLinkedList(from: [1, 4, 5, 7, 10]) + var list2: SinglyLinkedListNode? = try Helpers.generateLinkedList(from: [0, 3, 6, 8, 10, 11]) + var result = MergeLists.merge(head1: list1, head2: list2) + var values = Helpers.linkedListToArray(head: result) + #expect(values == [0, 1, 3, 4, 5, 6, 7, 8, 10, 10, 11]) + + list1 = nil + list2 = nil + result = MergeLists.merge(head1: list1, head2: list2) + #expect(result == nil) + + list1 = try Helpers.generateLinkedList(from: [1, 2, 3]) + list2 = nil + result = MergeLists.merge(head1: list1, head2: list2) + values = Helpers.linkedListToArray(head: result) + #expect(values == [1, 2, 3]) + } +} diff --git a/Tests/swift-ds-algorithms-tests/LinkedListTests/ReversseLinkedListTests.swift b/Tests/swift-ds-algorithms-tests/LinkedListTests/ReverseLinkedListTests.swift similarity index 100% rename from Tests/swift-ds-algorithms-tests/LinkedListTests/ReversseLinkedListTests.swift rename to Tests/swift-ds-algorithms-tests/LinkedListTests/ReverseLinkedListTests.swift