Skip to content

Instantly share code, notes, and snippets.

@harsh183
Last active April 17, 2021 11:03
Show Gist options
  • Save harsh183/180856e9842302cacf431c6bac9bbe59 to your computer and use it in GitHub Desktop.
Save harsh183/180856e9842302cacf431c6bac9bbe59 to your computer and use it in GitHub Desktop.
Simple singly linked list using data classes in Kotlin using one line. Featuring data classes and null safety
// Linked list
data class Node<T>(var value: T, var next: Node<T>?);
fun main() {
val head = Node(1, null)
val second = Node(2, Node(3, null)) // two more (init multiple)
head.next = second
println(head.value) // 1
println(head.next?.value) // 2
println(head.next?.next?.value) // 3
println(head.next?.next?.next?.value) // null
println(head)
// Node(value=1, next=Node(value=2, next=Node(value=3, next=null)))
}
@harsh183
Copy link
Author

harsh183 commented May 8, 2020

MIT License

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment