forked from walnutown/CodingInTheDeep
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTwoNumbers.java
More file actions
84 lines (80 loc) · 2.39 KB
/
Copy pathAddTwoNumbers.java
File metadata and controls
84 lines (80 loc) · 2.39 KB
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
/*
You are given two linked lists representing two non-negative numbers.
The digits are stored in reverse order and each of their nodes contain a single digit.
Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
// Maintain a carry
// time: O(m+n); space: O(1)
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (l1 == null || l2 == null)
return l1 == null ? l2 : l1;
ListNode p1 = l1, p2 = l2;
ListNode dum = new ListNode(0);
ListNode res = dum;
int carry = 0;
while (p1 != null && p2 != null){
res.next = new ListNode((p1.val + p2.val + carry)%10);
carry = (p1.val + p2.val + carry)/10;
res = res.next;
p1 = p1.next;
p2 = p2.next;
}
while (p1 != null){
res.next = new ListNode((p1.val + carry)%10);
carry = (p1.val + carry)/10;
res = res.next;
p1 = p1.next;
}
while (p2 != null){
res.next = new ListNode((p2.val + carry)%10);
carry = (p2.val + carry)/10;
res = res.next;
p2 = p2.next;
}
if (carry > 0) res.next = new ListNode(carry);
return dum.next;
}
}
// Combine the while loop to make code more concise
// time: O(m+n); space(1)
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (l1==null || l2==null){
if (l1==null && l2==null) return null;
return l1==null? l2: l1;
}
ListNode sen = new ListNode(0);
ListNode s = sen, p = l1, q = l2;
int carry = 0;
while (p!=null || q!=null){
int num1 = 0, num2 = 0;
if (p!=null){
num1 = p.val;
p = p.next;
}
if (q!=null){
num2 = q.val;
q = q.next;
}
s.next = new ListNode((num1+num2+carry)%10);
s = s.next;
carry = (num1+num2+carry)/10;
}
if (carry>0) s.next = new ListNode(carry);
return sen.next;
}
}