-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddTwoNumbers.cs
69 lines (55 loc) · 1.66 KB
/
AddTwoNumbers.cs
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
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode AddTwoNumbers (ListNode l1, ListNode l2) {
var l1Count = ListCount (l1);
var l2Count = ListCount (l2);
if (l1Count >= l2Count) {
return Add (l2, l1, l1Count);
} else {
return Add (l1, l2, l2Count);
}
}
public ListNode Add (ListNode smallList, ListNode biggerList, int bigCount) {
var currentBigList = biggerList;
var currentCarry = 0;
for (var i = 0; i < bigCount; i++) {
var newVal = currentBigList.val + smallList.val + currentCarry;
if (newVal >= 10) {
newVal -= 10;
currentCarry = 1;
} else {
currentCarry = 0;
}
currentBigList.val = newVal;
if (currentBigList.next != null) {
currentBigList = currentBigList.next;
} else {
if (currentCarry == 1) {
currentBigList.next = new ListNode (1);
}
}
if (smallList.next != null) {
smallList = smallList.next;
} else {
smallList = new ListNode (0);
}
}
return biggerList;
}
public int ListCount (ListNode ln) {
var count = 0;
var currentListNode = ln;
while (currentListNode != null) {
count++;
currentListNode = currentListNode.next;
}
return count;
}
}