-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-two-numbers-ii.java
93 lines (67 loc) · 2.02 KB
/
add-two-numbers-ii.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Problem link - https://leetcode.com/problems/add-two-numbers-ii/
/**
* Definition for singly-linked list.
* 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; }
* }
*/
// TC - O(m + n)
// SC - O(m + n) --- counting input list length in space complexity because it is being used in the algorithm, in deriving the answer
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
l1 = reverseList(l1);
l2 = reverseList(l2);
ListNode dummy = new ListNode(-1);
ListNode sum = dummy;
int carry = 0;
while(l1 != null && l2 != null){
int num = l1.val + l2.val + carry;
carry = (num / 10);
num = (num % 10);
sum.next = new ListNode(num);
sum = sum.next;
l1 = l1.next;
l2 = l2.next;
}
// if l1 has digits left
while(l1 != null){
int num = l1.val + carry;
carry = (num / 10);
num = (num % 10);
sum.next = new ListNode(num);
sum = sum.next;
l1 = l1.next;
}
//if l2 has digits left
while(l2 != null){
int num = l2.val + carry;
carry = (num / 10);
num = (num % 10);
sum.next = new ListNode(num);
sum = sum.next;
l2 = l2.next;
}
if(carry > 0){
sum.next = new ListNode(carry);
}
ListNode ans = reverseList(dummy.next);
return ans;
}
private ListNode reverseList(ListNode head){
if(head == null || head.next == null) return head;
ListNode p = head, c = head.next, n = head.next.next;
while(n != null){
c.next = p;
p = c;
c = n;
n = n.next;
}
c.next = p;
head.next = null;
return c;
}
}