Skip to content

Commit 1c6e93b

Browse files
optimize 2
1 parent ed4827e commit 1c6e93b

File tree

3 files changed

+36
-174
lines changed

3 files changed

+36
-174
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ Your ideas/fixes/algorithms are more than welcome!
424424
|5|[Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/)|[Solution](../master/src/main/java/com/stevesun/solutions/LongestPalindromicSubstring.java) | O(n^2) | O(1) | Medium|
425425
|4|[Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/)|[Solution](../master/src/main/java/com/stevesun/solutions/MedianofTwoSortedArrays.java) | ? | ? | Hard | Divide and Conquer
426426
|3|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)|[Solution](../master/src/main/java/com/stevesun/solutions/LongestSubstringWithoutRepeatingCharacters.java) | O(n) | O(1) | Medium | HashMap, Sliding Window
427-
|2|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/)|[Solution](../master/src/main/java/com/stevesun/solutions/AddTwoNumbers.java) | O(n) | O(1) | Medium | LinkedList
427+
|2|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/)|[Solution](../master/src/main/java/com/stevesun/solutions/_2.java) | O(max(m,n)) | O(1) | Medium | LinkedList
428428
|1|[Two Sum](https://leetcode.com/problems/two-sum/)|[Solution](../master/src/main/java/com/stevesun/solutions/_1.java)| O(n)/O(n^2)|O(1)/O(n) | Easy| HashMap
429429

430430
## Database

src/main/java/com/stevesun/solutions/AddTwoNumbers.java

Lines changed: 0 additions & 173 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.stevesun.solutions;
2+
3+
import com.stevesun.common.classes.ListNode;
4+
import com.stevesun.common.utils.CommonUtils;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
/**2. Add Two Numbers
10+
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.
11+
12+
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
13+
Output: 7 -> 0 -> 8*/
14+
public class _2 {
15+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
16+
ListNode result = new ListNode(0);
17+
ListNode tmp = result;
18+
int sum = 0;
19+
while (l1 != null || l2 != null) {
20+
sum /= 10;
21+
if (l1 != null) {
22+
sum += l1.val;
23+
l1 = l1.next;
24+
}
25+
if (l2 != null) {
26+
sum += l2.val;
27+
l2 = l2.next;
28+
}
29+
tmp.next = new ListNode(sum%10);;
30+
tmp = tmp.next;
31+
}
32+
if (sum/10 == 1) tmp.next = new ListNode(1);//this means there's a carry, so we add additional 1, e.g. [5] + [5] = [0, 1]
33+
return result.val == 0 ? result.next : result;
34+
}
35+
}

0 commit comments

Comments
 (0)