diff --git a/src/main/kotlin/data_structures/linked_list/LinkedList.kt b/src/main/kotlin/data_structures/linked_list/LinkedList.kt index aad9c2a..a816f68 100644 --- a/src/main/kotlin/data_structures/linked_list/LinkedList.kt +++ b/src/main/kotlin/data_structures/linked_list/LinkedList.kt @@ -198,6 +198,8 @@ class LinkedList : Iterable> { override fun iterator(): Iterator> { return NodeIterator(this) } + + companion object } /** @@ -210,3 +212,22 @@ inline fun LinkedList.toArray(): Array { } return newArray } + +/** + * Creates an empty linked list. + */ +fun LinkedList.Companion.emptyList(): LinkedList { + return LinkedList() +} + +/** + * Constructs a linked list from a variable number of arguments. + */ +fun LinkedList.Companion.listOf(vararg items: T): LinkedList { + val linkedList = LinkedList() + items.forEach { + linkedList.append(it) + } + return linkedList +} + diff --git a/src/test/kotlin/data_structures/linked_list/LinkedListTest.kt b/src/test/kotlin/data_structures/linked_list/LinkedListTest.kt index 5f0d0f1..5320652 100644 --- a/src/test/kotlin/data_structures/linked_list/LinkedListTest.kt +++ b/src/test/kotlin/data_structures/linked_list/LinkedListTest.kt @@ -1,6 +1,8 @@ package data_structures.linked_list import dev.nuculabs.dsa.data_structures.linked_list.LinkedList +import dev.nuculabs.dsa.data_structures.linked_list.emptyList +import dev.nuculabs.dsa.data_structures.linked_list.listOf import dev.nuculabs.dsa.data_structures.linked_list.toArray import org.junit.jupiter.api.Test @@ -289,4 +291,21 @@ class LinkedListTest { // Assert assertContentEquals(arrayOf("First", "Second"), result) } + + @Test + fun emptyList() { + // Test + val list = LinkedList.emptyList() + // Assert + assertEquals(0, list.size()) + } + + @Test + fun listOf() { + // Test + val list = LinkedList.listOf("One", "Two", "Three") + // Assert + assertEquals(3, list.size()) + assertContentEquals(listOf("One", "Two", "Three"), list.toJavaList()) + } } \ No newline at end of file