48
48
49
49
class Solution :
50
50
def addTwoNumbers (self , l1 : ListNode, l2 : ListNode) -> ListNode:
51
- p = ListNode(- 1 )
52
- carry, t = 0 , p
53
- while l1 or l2:
54
- s = (0 if l1 is None else l1.val) + (0 if l2 is None else l2.val) + carry
55
- carry = 1 if s > 9 else 0
56
- t.next = ListNode(s % 10 )
57
- t = t.next
58
- l1 = l1.next if l1 else l1
59
- l2 = l2.next if l2 else l2
60
- t.next = None if carry == 0 else ListNode(carry)
61
- return p.next
62
-
63
-
51
+ carry = 0
52
+ dummy = ListNode(- 1 )
53
+ cur = dummy
54
+ while l1 or l2 or carry:
55
+ s = (0 if not l1 else l1.val) + (0 if not l2 else l2.val) + carry
56
+ carry, val = divmod (s, 10 )
57
+ cur.next = ListNode(val)
58
+ cur = cur.next
59
+ l1 = None if not l1 else l1.next
60
+ l2 = None if not l2 else l2.next
61
+ return dummy.next
64
62
```
65
63
66
64
### ** Java**
@@ -78,23 +76,83 @@ class Solution:
78
76
*/
79
77
class Solution {
80
78
public ListNode addTwoNumbers (ListNode l1 , ListNode l2 ) {
81
- ListNode p = new ListNode (- 1 );
82
79
int carry = 0 ;
83
- ListNode t = p;
84
- while (l1 != null || l2 != null ) {
80
+ ListNode dummy = new ListNode (- 1 );
81
+ ListNode cur = dummy;
82
+ while (l1 != null || l2 != null || carry != 0 ) {
85
83
int s = (l1 == null ? 0 : l1. val) + (l2 == null ? 0 : l2. val) + carry;
86
- t . next = new ListNode (s % 10 ) ;
87
- carry = s > 9 ? 1 : 0 ;
88
- t = t . next;
89
- l1 = l1 == null ? l1 : l1. next;
90
- l2 = l2 == null ? l2 : l2. next;
84
+ carry = s / 10 ;
85
+ cur . next = new ListNode (s % 10 ) ;
86
+ cur = cur . next;
87
+ l1 = l1 == null ? null : l1. next;
88
+ l2 = l2 == null ? null : l2. next;
91
89
}
92
- t. next = carry == 0 ? null : new ListNode (carry);
93
- return p. next;
90
+ return dummy. next;
94
91
}
95
92
}
96
93
```
97
94
95
+ ### ** C++**
96
+
97
+ ``` cpp
98
+ /* *
99
+ * Definition for singly-linked list.
100
+ * struct ListNode {
101
+ * int val;
102
+ * ListNode *next;
103
+ * ListNode(int x) : val(x), next(NULL) {}
104
+ * };
105
+ */
106
+ class Solution {
107
+ public:
108
+ ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
109
+ int carry = 0;
110
+ ListNode* dummy = new ListNode(-1);
111
+ ListNode* cur = dummy;
112
+ while (l1 != NULL || l2 != NULL || carry != 0) {
113
+ int s = (l1 == NULL ? 0 : l1-> val) + (l2 == NULL ? 0 : l2->val) + carry;
114
+ carry = s / 10;
115
+ cur->next = new ListNode(s % 10);
116
+ cur = cur->next;
117
+ l1 = l1 == NULL ? NULL : l1->next;
118
+ l2 = l2 == NULL ? NULL : l2->next;
119
+ }
120
+ return dummy->next;
121
+ }
122
+ };
123
+ ```
124
+
125
+ ### **JavaScript**
126
+
127
+ ```js
128
+ /**
129
+ * Definition for singly-linked list.
130
+ * function ListNode(val) {
131
+ * this.val = val;
132
+ * this.next = null;
133
+ * }
134
+ */
135
+ /**
136
+ * @param {ListNode} l1
137
+ * @param {ListNode} l2
138
+ * @return {ListNode}
139
+ */
140
+ var addTwoNumbers = function (l1, l2) {
141
+ let carry = 0;
142
+ const dummy = new ListNode(-1);
143
+ let cur = dummy;
144
+ while (l1 || l2 || carry) {
145
+ const s = (l1 ? l1.val : 0) + (l2 ? l2.val : 0) + carry;
146
+ carry = Math.floor(s / 10);
147
+ cur.next = new ListNode(s % 10);
148
+ cur = cur.next;
149
+ l1 = l1 ? l1.next : l1;
150
+ l2 = l2 ? l2.next : l2;
151
+ }
152
+ return dummy.next;
153
+ };
154
+ ```
155
+
98
156
### ** ...**
99
157
100
158
```
0 commit comments