Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 48e1fd2

Browse files
committed
feat: add leetcode question #449
1 parent 973983f commit 48e1fd2

File tree

2 files changed

+2
-14
lines changed

2 files changed

+2
-14
lines changed

‎00-code(源代码)/src/com/hi/dhl/algorithms/leetcode/_449/java/Solution.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
2525
l1 = reverse(l1);
2626
l2 = reverse(l2);
2727
int carry = 0;
28-
while (l1 != null || l2 != null) {
28+
while (l1 != null || l2 != null || carry > 0) {
2929
int a = l1 == null ? 0 : l1.val;
3030
int b = l2 == null ? 0 : l2.val;
3131
int sum = a + b + carry;
@@ -36,12 +36,6 @@ public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
3636
if (l1 != null) l1 = l1.next;
3737
if (l2 != null) l2 = l2.next;
3838
}
39-
40-
if (carry != 0) {
41-
ListNode node = new ListNode(carry);
42-
node.next = head;
43-
head = node;
44-
}
4539
return head;
4640
}
4741

‎00-code(源代码)/src/com/hi/dhl/algorithms/leetcode/_449/kotlin/Solution.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Solution {
2424
var p2 = reverse(l2)
2525
var head: ListNode? = null;
2626
var carry = 0;
27-
while(p1 != null || p2 != null){
27+
while(p1 != null || p2 != null|| carry >0){
2828
val a = p1?.`val` ?:0
2929
val b = p2?.`val` ?:0
3030
val sum = a +b + carry
@@ -35,12 +35,6 @@ class Solution {
3535
p1 = p1?.next
3636
p2 = p2?.next
3737
}
38-
39-
if(carry != 0){
40-
val node = ListNode(carry)
41-
node.next = head
42-
head = node
43-
}
4438
return head
4539
}
4640

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /