-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTwoNumbers.cs
More file actions
38 lines (38 loc) · 1.12 KB
/
Copy pathAddTwoNumbers.cs
File metadata and controls
38 lines (38 loc) · 1.12 KB
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
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0, null);
var current = head;
int balance = 0;
while(l1 is not null || l2 is not null || balance is not 0){
current.next = new ListNode();
current = current.next;
if(l1 != null && l2 != null){
balance = balance + l1.val + l2.val;
l1 = l1.next;
l2 = l2.next;
}
else if(l1 is not null && l2 is null){
balance = balance + l1.val;
l1 = l1.next;
}
else if(l2 is not null && l1 is null){
balance = balance + l2.val;
l2 = l2.next;
}
current.val = balance % 10;
balance = balance / 10;
}
return head.next;
}
}