MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / Solution

Class Solution

src/com/blankj/medium/_0002/Solution.java:13–42  ·  view source on GitHub ↗

author: Blankj blog : http://blankj.com time : 2017/10/11 desc :

Source from the content-addressed store, hash-verified

11 * </pre>
12 */
13public class Solution {
14 public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
15 ListNode node = new ListNode(0);
16 ListNode n1 = l1, n2 = l2, t = node;
17 int sum = 0;
18 while (n1 != null || n2 != null) {
19 sum /= 10;
20 if (n1 != null) {
21 sum += n1.val;
22 n1 = n1.next;
23 }
24 if (n2 != null) {
25 sum += n2.val;
26 n2 = n2.next;
27 }
28 t.next = new ListNode(sum % 10);
29 t = t.next;
30 }
31 if (sum / 10 != 0) t.next = new ListNode(1);
32 return node.next;
33 }
34
35 public static void main(String[] args) {
36 Solution solution = new Solution();
37 ListNode.print(solution.addTwoNumbers(
38 ListNode.createTestData("[2,4,3]"),
39 ListNode.createTestData("[5,6,4]")
40 ));
41 }
42}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected