We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents ab1a8c3 + f510a68 commit f08097fCopy full SHA for f08097f
leetcode/java/linkedlist/2.java
@@ -0,0 +1,21 @@
1
+class Solution {
2
+ public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
3
+ ListNode root = new ListNode();
4
+ ListNode currentNode = root;
5
+ int carry = 0;
6
+ while (l1 != null || l2 != null){
7
+ int val1 = l1 != null ? l1.val : 0;
8
+ int val2 = l2 != null ? l2.val : 0;
9
+ int sum = val1 + val2 + carry;
10
+ carry = sum / 10;
11
+ currentNode.next = new ListNode(sum % 10);
12
+ currentNode = currentNode.next;
13
+ l1 = l1 != null ? l1.next : null;
14
+ l2 = l2 != null ? l2.next : null;
15
+ }
16
+ if (carry == 1){
17
+ currentNode.next = new ListNode(carry);
18
19
+ return root.next;
20
21
+}
0 commit comments