-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAddTwoNumbersLL.java
More file actions
34 lines (29 loc) · 839 Bytes
/
AddTwoNumbersLL.java
File metadata and controls
34 lines (29 loc) · 839 Bytes
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
//TC: O(N) to go throguh the length of the longest LL
//SC: O(1)
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1 == null || l2 == null) return null;
ListNode dummy = new ListNode(-1);
ListNode curr = dummy;
int carry =0;
while(l1 != null || l2!=null){
int total = 0;
if(l1!=null){
total+=l1.val;
l1=l1.next;
}
if(l2!=null){
total+=l2.val;
l2=l2.next;
}
total+=carry;
curr.next = new ListNode(total%10);
carry=total/10;
curr = curr.next;
}
if(carry > 0){
curr.next = new ListNode(carry);
}
return dummy.next;
}
}