1
- package org .ginryan .github .leetcodes .s2_add_two_numbers ;
2
-
3
- import java .util .ArrayList ;
4
-
5
- /**
6
- *
7
- * 给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
8
- *
9
- * 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
10
- *
11
- * 您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
12
- *
13
- * 示例:
14
- *
15
- * 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
16
- *
17
- * 输出:7 -> 0 -> 8
18
- *
19
- * 原因:342 + 465 = 807
20
- *
21
- * @author GinRyan
22
- */
23
- class Solution {
24
-
25
- public ListNode addTwoNumbers (ListNode l1 , ListNode l2 ) {
26
- ListNode retListNode = new ListNode (0 );
27
- ListNode latestRetListNode = retListNode ;
28
- ListNode currentNode1 = l1 ;
29
- ListNode currentNode2 = l2 ;
30
- // 满位进制准备值
31
- int carry = 0 ;
32
-
33
- while (currentNode1 != null || currentNode2 != null ) {
34
- // 一旦遇到单个空节点,则说明高位为0,则填充高位为0
35
- currentNode1 = currentNode1 == null ? new ListNode (0 ) : currentNode1 ;
36
- currentNode2 = currentNode2 == null ? new ListNode (0 ) : currentNode2 ;
37
- // 新建一个节点,并且填充两数字之和,并且加上上一节点进位值。如无进位这个值为0
38
- int cSum = currentNode1 .val + currentNode2 .val + carry ;
39
- // 取余做填充数值
40
- int cMod = cSum % 10 ;
41
- // 取整除值做进位值写入到to_upper_pos变量中,用于下次将进位值加和
42
- carry = cSum / 10 ;
43
-
44
- ListNode createMode = new ListNode (cMod );
45
- //挂接到结果链表节点下
46
- latestRetListNode .next = createMode ;
47
- //挂接到结果链表节点下之后还要向前推latestRetListNode到下一个节点
48
- latestRetListNode = latestRetListNode .next ;
49
- //次级节点递送
50
- currentNode1 = currentNode1 .next ;
51
- currentNode2 = currentNode2 .next ;
52
-
53
- }
54
- //最高位如果升位,需要增加一位节点,放1
55
- if (carry > 0 ) {
56
- ListNode createMode = new ListNode (carry );
57
- latestRetListNode .next = createMode ;
58
- }
59
- return retListNode .next ;
60
- }
61
-
62
- public static void main (String [] args ) {
63
-
64
- }
65
- }
66
-
67
- class ListNode {
68
- int val ;
69
- ListNode next ;
70
-
71
- ListNode (int x ) {
72
- val = x ;
73
- }
1
+ package org .ginryan .github .leetcodes .s2_add_two_numbers ;
2
+
3
+ import java .util .ArrayList ;
4
+
5
+ /**
6
+ *
7
+ * 给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
8
+ *
9
+ * 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
10
+ *
11
+ * 您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
12
+ *
13
+ * 示例:
14
+ *
15
+ * 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
16
+ *
17
+ * 输出:7 -> 0 -> 8
18
+ *
19
+ * 原因:342 + 465 = 807
20
+ *
21
+ * @author GinRyan
22
+ */
23
+ class Solution {
24
+
25
+ public ListNode addTwoNumbers (ListNode l1 , ListNode l2 ) {
26
+ ListNode retListNode = new ListNode (0 );
27
+ ListNode latestRetListNode = retListNode ;
28
+ ListNode currentNode1 = l1 ;
29
+ ListNode currentNode2 = l2 ;
30
+ // 满位进制准备值
31
+ int carry = 0 ;
32
+
33
+ while (currentNode1 != null || currentNode2 != null ) {
34
+ // 一旦遇到单个空节点,则说明高位为0,则填充高位为0
35
+ currentNode1 = currentNode1 == null ? new ListNode (0 ) : currentNode1 ;
36
+ currentNode2 = currentNode2 == null ? new ListNode (0 ) : currentNode2 ;
37
+ // 新建一个节点,并且填充两数字之和,并且加上上一节点进位值。如无进位这个值为0
38
+ int cSum = currentNode1 .val + currentNode2 .val + carry ;
39
+ // 取余做填充数值
40
+ int cMod = cSum % 10 ;
41
+ // 取整除值做进位值写入到to_upper_pos变量中,用于下次将进位值加和
42
+ carry = cSum / 10 ;
43
+
44
+ ListNode createMode = new ListNode (cMod );
45
+ //挂接到结果链表节点下
46
+ latestRetListNode .next = createMode ;
47
+ //挂接到结果链表节点下之后还要向前推latestRetListNode到下一个节点
48
+ latestRetListNode = latestRetListNode .next ;
49
+ //次级节点递送
50
+ currentNode1 = currentNode1 .next ;
51
+ currentNode2 = currentNode2 .next ;
52
+
53
+ }
54
+ //最高位如果升位,需要增加一位节点,放1
55
+ if (carry > 0 ) {
56
+ ListNode createMode = new ListNode (carry );
57
+ latestRetListNode .next = createMode ;
58
+ }
59
+ return retListNode .next ;
60
+ }
61
+
62
+ public static void main (String [] args ) {
63
+
64
+ }
65
+ }
66
+
67
+ class ListNode {
68
+ int val ;
69
+ ListNode next ;
70
+
71
+ ListNode (int x ) {
72
+ val = x ;
73
+ }
74
74
}
0 commit comments