File tree 4 files changed +134
-122
lines changed
src/com/blankj/medium/_002
4 files changed +134
-122
lines changed Original file line number Diff line number Diff line change 46
46
47
47
| #| Title| Tag|
48
48
| :------------- | :------------- | :------------- |
49
- | 2| Add Two Numbers| Linked List, Math|
49
+ | 2| [ Add Two Numbers] [ 002 ] | Linked List, Math|
50
50
| 8| [ String to Integer (atoi)] [ 008 ] | Math, String|
51
51
| 19| [ Remove Nth Node From End of List] [ 019 ] | Linked List, Two Pointers|
52
52
96
96
[ 121 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/121/README.md
97
97
[ 122 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/122/README.md
98
98
99
+ [ 002 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/002/README.md
99
100
[ 008 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/008/README.md
100
101
[ 019 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/019/README.md
102
+
103
+ [ 004 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/004/README.md
Original file line number Diff line number Diff line change
1
+ # [ Add Two Numbers] [ title ]
2
+
3
+ ## Description
4
+
5
+ You are given two ** non-empty** linked lists representing two non-negative integers. 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.
6
+
7
+ You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8
+
9
+ ** Input:** (2 -> 4 -> 3) + (5 -> 6 -> 4)
10
+ ** Output:** 7 -> 0 -> 8
11
+
12
+ ** Tags:** Linked List, Math
13
+
14
+
15
+ ## 思路
16
+
17
+ 题意我也是看了好久才看懂,就是以链表表示一个数,低位在前,高位在后,所以题中的例子就是` 342 + 465 = 807 ` ,所以我们模拟计算即可。
18
+
19
+ ``` java
20
+ /**
21
+ * Definition for singly-linked list.
22
+ * public class ListNode {
23
+ * int val;
24
+ * ListNode next;
25
+ * ListNode(int x) { val = x; }
26
+ * }
27
+ */
28
+ class Solution {
29
+ public ListNode addTwoNumbers (ListNode l1 , ListNode l2 ) {
30
+ ListNode node = new ListNode (0 );
31
+ ListNode n1 = l1, n2 = l2, t = node;
32
+ int sum = 0 ;
33
+ while (n1 != null || n2 != null ) {
34
+ sum /= 10 ;
35
+ if (n1 != null ) {
36
+ sum += n1. val;
37
+ n1 = n1. next;
38
+ }
39
+ if (n2 != null ) {
40
+ sum += n2. val;
41
+ n2 = n2. next;
42
+ }
43
+ t. next = new ListNode (sum % 10 );
44
+ t = t. next;
45
+ }
46
+ if (sum / 10 != 0 ) t. next = new ListNode (1 );
47
+ return node. next;
48
+ }
49
+ }
50
+ ```
51
+
52
+
53
+ ## 结语
54
+
55
+ 如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[ awesome-java-leetcode] [ ajl ]
56
+
57
+
58
+
59
+ [ title ] : https://leetcode.com/problems/add-two-numbers
60
+ [ ajl ] : https://github.com/Blankj/awesome-java-leetcode
Original file line number Diff line number Diff line change
1
+ package com .blankj .medium ._002 ;
2
+
3
+ import com .blankj .structure .ListNode ;
4
+
5
+ import java .util .Arrays ;
6
+
7
+ /**
8
+ * <pre>
9
+ * author: Blankj
10
+ * blog : http://blankj.com
11
+ * time : 2017/10/11
12
+ * desc :
13
+ * </pre>
14
+ */
15
+ public class Solution {
16
+
17
+ public ListNode addTwoNumbers (ListNode l1 , ListNode l2 ) {
18
+
19
+ }
20
+
21
+
22
+ public static void main (String [] args ) {
23
+ Solution solution = new Solution ();
24
+
25
+ System .out .println (Arrays .toString (solution .addTwoNumbers (nums , target )));
26
+ }
27
+ }
You can’t perform that action at this time.
0 commit comments