특징

장점과 단점

예시 코드

class ListNode<T>(var value: T, var next: ListNode<T>? = null)

fun <T> addNode(head: ListNode<T>?, value: T): ListNode<T> {
    if (head == null) return ListNode(value)
    var current = head
    while (current?.next != null) {
        current = current.next
    }
    current?.next = ListNode(value)
    return head
}

fun <T> printList(head: ListNode<T>?) {
    var current = head
    while (current != null) {
        print("${current.value} ")
        current = current.next
    }
    println()
}

var head: ListNode<Int>? = ListNode(1)
head = addNode(head, 2)
head = addNode(head, 3)
printList(head)