美文网首页
LeetCode之Add Two Numbers(Kotlin)

LeetCode之Add Two Numbers(Kotlin)

作者: 糕冷羊 | 来源:发表于2021-01-14 14:17 被阅读0次

问题:



方法:
创建一个新的链表,每次创建的节点等于l1的节点加l2的节点加进位数,l1和l2不等长的情况需要特殊处理,结束条件需要特殊处理。

class AddTwoNumbers {
    fun addTwoNumbers(l1: ListNode?, l2: ListNode?): ListNode? {
        val root = ListNode(0)
        var left = l1
        var right = l2
        var cur: ListNode? = root
        var red = 0
        while (left != null || right != null) {
            if (left != null && right != null) {
                cur?.`val` = (left.`val` + right.`val` + red) % 10
                red = (left.`val` + right.`val` + red) / 10
            } else if (left != null) {
                cur?.`val` = (left.`val` + red) % 10
                red = (left.`val` + red) / 10
            } else if (right != null) {
                cur?.`val` = (right.`val` + red) % 10
                red = (right.`val` + red) / 10
            }

            if (left?.next != null || right?.next != null) {
                cur?.next = ListNode(0)
            } else if (red != 0) {
                cur?.next = ListNode(red)
            }
            cur = cur?.next
            left = left?.next
            right = right?.next
        }
        return root
    }
}

fun main() {
    val l1 = ListNode(2)
    l1.next = ListNode(4)
    l1.next!!.next = ListNode(3)
    val l2 = ListNode(5)
    l2.next = ListNode(6)
    l2.next!!.next = ListNode(4)
    val addTwoNumbers = AddTwoNumbers()
    addTwoNumbers.addTwoNumbers(l1, l2)
}

有问题随时沟通

具体代码实现可以参考Github

相关文章

网友评论

      本文标题:LeetCode之Add Two Numbers(Kotlin)

      本文链接:https://www.haomeiwen.com/subject/mrrvaktx.html