-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
35 lines (32 loc) · 998 Bytes
/
Main.java
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
package com.company;
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public class Main {
public static void main(String[] args) {
// write your code here
}
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
var dummy = new ListNode();
var point = dummy;
int cur = 0, car = 0;
while (l1 != null && l2 != null){
cur = (l1.val + l2.val + car) % 10;
car = (l1.val + l2.val + car) / 10;
point.next = new ListNode(cur);
l1 = l1.next;
l2 = l2.next;
point = point.next;
if(l1 == null && l2 != null)
l1 = new ListNode();
if(l1 != null && l2 == null)
l2 = new ListNode();
}
if(car == 1) point.next = new ListNode(car);
return dummy.next;
}
}