-
Notifications
You must be signed in to change notification settings - Fork 0
/
Add Two Numbers.kt
72 lines (67 loc) · 1.95 KB
/
Add Two Numbers.kt
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
*
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
*
*
*/
/**
* Definition for singly-linked list.
* class ListNode(var `val`: Int = 0) {
* var next: ListNode? = null
* }
*/
class ListNode(var `val`:Int = 0){
var next: ListNode? = null
}
class Solution {
fun addTwoNumbers(l1: ListNode?, l2: ListNode?): ListNode? {
var list1 = l1
var list2 = l2
var sum = 0
var tempSum = 0
var pre1:ListNode? = null
while (list1!=null&&list2!=null){
list1.`val`+=(sum/10)
sum = list1.`val` + list2.`val`
tempSum = sum%10
list1.`val` = tempSum
pre1 = list1
list1 = list1.next
list2 = list2.next
}
if(list2 == null && list1 == null){
if(sum>=10){
val node = ListNode(sum/10)
pre1!!.next = node
node.next = null
}else{
pre1!!.`val` += (sum/10)
}
}else if (list1 == null){
addNumberIntoList(list2,sum/10)
pre1!!.next = list2
}else{
addNumberIntoList(list1,sum/10)
}
return l1
}
fun addNumberIntoList(list: ListNode?, value:Int ){
var l = list
l!!.`val` += value
while (l!=null&&l.`val`>=10){
val temp = l.`val`
l.`val` = temp%10
if (l.next!=null){
l.next!!.`val` += (temp/10)
}else{
val node = ListNode(temp/10)
l.next = node
node.next = null
}
l = l.next
}
}
}