Skip to content

Commit 38a07d8

Browse files
committed
add-two-numbers.cs
1 parent 77c8141 commit 38a07d8

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public int val;
5+
* public ListNode next;
6+
* public ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
public class Solution {
10+
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
11+
ListNode dummy = new ListNode(0);
12+
ListNode current = dummy;
13+
int carry = 0;
14+
15+
while (l1 != null || l2 != null || carry != 0) {
16+
int sum = carry;
17+
if (l1 != null) {
18+
sum += l1.val;
19+
l1 = l1.next;
20+
}
21+
if (l2 != null) {
22+
sum += l2.val;
23+
l2 = l2.next;
24+
}
25+
26+
carry = sum / 10;
27+
sum = sum % 10;
28+
current.next = new ListNode(sum);
29+
current = current.next;
30+
}
31+
32+
return dummy.next;
33+
}
34+
}

2-add-two-numbers/2-add-two-numbers.js

Whitespace-only changes.

2-add-two-numbers/2-add-two-numbers.php

Whitespace-only changes.

2-add-two-numbers/2-add-two-numbers.py

Whitespace-only changes.

2-add-two-numbers/2-add-two-numbers.rb

Whitespace-only changes.

0 commit comments

Comments
 (0)