diff --git a/.idea/misc.xml b/.idea/misc.xml index cb3160a..bcb5da6 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/KtLeetcode.iml b/KtLeetcode.iml index f889b2e..43dd653 100644 --- a/KtLeetcode.iml +++ b/KtLeetcode.iml @@ -8,7 +8,7 @@ - + diff --git a/src/Main.kt b/src/Main.kt index e62d862..911c4b0 100644 --- a/src/Main.kt +++ b/src/Main.kt @@ -2,6 +2,6 @@ fun main() { - - + val t = Test3() + t.test() } \ No newline at end of file diff --git a/src/Test2.kt b/src/Test2.kt index 9a13a3f..b672f85 100644 --- a/src/Test2.kt +++ b/src/Test2.kt @@ -4,25 +4,52 @@ class Test2 { var next: ListNode? = null } - fun addTwoNumbers(l1: ListNode?, l2: ListNode?): ListNode? { - val cf = false - val result = ListNode(1) - if (l1 != null) { - if (l2!=null){ + fun getVal(l: ListNode?): Int { + return l?.`val` ?: 0 + } - } + fun addTwoNumbers(l1: ListNode?, l2: ListNode?): ListNode? { + var cf = 0 + var l1 = l1 + var l2 = l2 + val result: ListNode = ListNode(0) + var current: ListNode? = result + + + while (l1!=null||l2!=null){ + val t = getVal(l1) + getVal(l2) + cf + cf = t/10 + current?.next = ListNode(t%10) + current = current?.next + l1 = l1?.next + l2 = l2?.next } - return null + + if (cf == 1){ + current?.next = ListNode(1) + } + + return result.next } fun test(){ - val li1 = ListNode(2) - li1.next = ListNode(4) - li1.next!!.next = ListNode(3) + val li1 = ListNode(9) + li1.next = ListNode(9) + li1.next!!.next = ListNode(9) + li1.next!!.next!!.next = ListNode(9) + + + val li2 = ListNode(9) + li2.next = ListNode(9) + li2.next!!.next = ListNode(9) + + var result = addTwoNumbers(li1,li2) + while (result!=null){ + print(result.`val`) + print(",") + result=result.next + } - val li2 = ListNode(5) - li2.next = ListNode(6) - li2.next!!.next = ListNode(4) } } \ No newline at end of file diff --git a/src/Test3.kt b/src/Test3.kt new file mode 100644 index 0000000..059a601 --- /dev/null +++ b/src/Test3.kt @@ -0,0 +1,10 @@ +class Test3 { + + fun lengthOfLongestSubstring(s: String): Int { + + } + + fun test(){ + + } +} \ No newline at end of file