-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-2-Add-Two-Numbers.java
More file actions
135 lines (108 loc) · 3.83 KB
/
LeetCode-2-Add-Two-Numbers.java
File metadata and controls
135 lines (108 loc) · 3.83 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
LeetCode: https://leetcode.com/problems/add-two-numbers/
LintCode: http://www.lintcode.com/problem/add-two-numbers/
JiuZhang: http://www.jiuzhang.com/solutions/add-two-numbers/
ProgramCreek: http://www.programcreek.com/2012/12/add-two-numbers/
Analysis:
Building a new list to store the result. Using a dummy node to record the pre-node of head in new list. Curr node to record the current pos.
Be careful: the last add of last two node may generate a '1', which need a new node.
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
// public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// if(l1 == null || l2 == null) return null;
// ListNode dummy = new ListNode(0);
// ListNode curr = dummy;
// ListNode p1 = l1, p2 = l2;
// int carry = 0;
// while(p1 != null || p2 != null){
// if(p1 != null){
// carry += p1.val;
// p1 = p1.next;
// }
// if(p2 != null){
// carry += p2.val;
// p2 = p2.next;
// }
// curr.next = new ListNode(carry % 10);
// carry = carry / 10;
// curr = curr.next;
// }
// if(carry == 1){
// curr.next = new ListNode(1);
// curr = curr.next;
// }
// return dummy.next;
// }
// public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// int carry = 0;
// ListNode dummy = new ListNode(-1);
// ListNode p = dummy;
// while(l1 != null && l2 != null) {
// int temp = carry;
// carry = (l1.val + l2.val + temp) / 10;
// ListNode curr = new ListNode((l1.val + l2.val + temp) % 10);
// p.next = curr;
// p = p.next;
// l1 = l1.next;
// l2 = l2.next;
// }
// while (l1 != null) {
// int temp = carry;
// carry = (l1.val + temp) / 10;
// ListNode curr = new ListNode((l1.val + temp) % 10);
// p.next = curr;
// p = p.next;
// l1 = l1.next;
// }
// while (l2 != null) {
// int temp = carry;
// carry = (l2.val + temp) / 10;
// ListNode curr = new ListNode((l2.val + temp) % 10);
// p.next = curr;
// p = p.next;
// l2 = l2.next;
// }
// if (carry != 0) {
// ListNode curr = new ListNode(carry);
// p.next = curr;
// }
// return dummy.next;
// }
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int carry = 0;
ListNode dummy = new ListNode(-1);
ListNode pre = dummy;
while(l1 != null && l2 != null) {
int val = (l1.val + l2.val + carry) % 10;
carry = (l1.val + l2.val + carry) / 10;
pre.next = new ListNode(val);
l1 = l1.next;
l2 = l2.next;
pre = pre.next;
}
while (l1 != null) {
int val = (l1.val + carry) % 10;
carry = (l1.val + carry) / 10;
pre.next = new ListNode(val);
l1 = l1.next;
pre = pre.next;
}
while(l2 != null) {
int val = (l2.val + carry) % 10;
carry = (l2.val + carry) / 10;
pre.next = new ListNode(val);
l2 = l2.next;
pre = pre.next;
}
if (carry != 0) pre.next = new ListNode(carry);
return dummy.next;
}
}