문제로

풀이

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
 * Example:
 * var li = ListNode(5)
 * var v = li.`val`
 * Definition for singly-linked list.
 * class ListNode(var `val`: Int) {
 *     var next: ListNode? = null
 * }
 */
class Solution {
    fun mergeTwoLists(list1: ListNode?, list2: ListNode?): ListNode? {
        if(list1 == null && list2 == null) return null
        else if(list1 == null) return list2
        else if(list2 == null) return list1

        var a = list1;
        var b = list2;

        val answer = ListNode(0)
        var temp: ListNode = answer

        while (a != null && b != null) {
            if(a.`val` <= b.`val`) {
                temp.next = a
                a = a.next
            } else {
                temp.next = b
                b = b.next
            }
            temp = temp.next!!
        }

        temp.next = a ?: b

        return answer.next
    }
}


재귀형태 풀이도 기억해두자 ...

풀이2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
    fun mergeTwoLists(list1: ListNode?, list2: ListNode?): ListNode? {
        if (list1 == null) return list2
        if (list2 == null) return list1

        return if (list1.`val` <= list2.`val`) {
            list1.next = mergeTwoLists(list1.next, list2)
            list1
        } else {
            list2.next = mergeTwoLists(list1, list2.next)
            list2
        }
    }
}