-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedAddTwoNumbers.java
119 lines (112 loc) · 2.76 KB
/
LinkedAddTwoNumbers.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package leetCode;
import java.util.Scanner;
/**
* @className LinkedAddTwoNumbers.java
* @author SakrA
* @version 创建时间:2019年3月20日 下午3:04:16
*/
public class LinkedAddTwoNumbers {
public static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode addList = new ListNode(0);
ListNode p = addList;
int carryBit = 0;
int sum = 0;
while (l1 != null && l2 != null) {
sum = l1.val + l2.val + carryBit;
carryBit = sum / 10;
l1 = l1.next;
l2 = l2.next;
p.val = sum % 10;
if (!(l1 == null && l2 == null) || (l1 == null && l2 == null && carryBit != 0))
p.next = new ListNode(0);
p = p.next;
if (l1 == null && l2 == null && carryBit != 0) {
p.val = carryBit;
p.next = null;
}
}
while (l2 != null) {
sum = l2.val + carryBit;
carryBit = sum / 10;
p.val = sum % 10;
l2 = l2.next;
if (l2 != null || (l2 == null && carryBit != 0))
p.next = new ListNode(0);
p = p.next;
}
while (l1 != null) {
sum = l1.val + carryBit;
carryBit = sum / 10;
p.val = sum % 10;
l1 = l1.next;
if (l1 != null || (l1 == null && carryBit != 0))
p.next = new ListNode(0);
p = p.next;
}
if (carryBit != 0) {
p.val = carryBit;
p.next = null;
}
return addList;
}
// 优秀代码示例
public ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode p = l1, q = l2, curr = dummyHead;
int carry = 0;
while (p != null || q != null) {
int x = (p != null ? p.val : 0);
int y = (q != null ? q.val : 0);
int sum = carry + x + y;
carry = sum / 10;
curr.next = new ListNode(sum % 10);
curr = curr.next;
if (p != null)
p = p.next;
if (q != null)
q = q.next;
}
if (carry > 0) {
curr.next = new ListNode(carry);
}
return dummyHead.next;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String numStr = sc.nextLine();
String[] num1 = numStr.split(" ");
ListNode l1 = new ListNode(0);
ListNode item1 = l1;
for (int i = 0; i < num1.length; i++) {
item1.val = Integer.parseInt(num1[i]);
if (i != num1.length - 1)
item1.next = new ListNode(0);
item1 = item1.next;
}
String numStr2 = sc.nextLine();
String[] num2 = numStr2.split(" ");
ListNode l2 = new ListNode(0);
ListNode item2 = l2;
for (int j = 0; j < num2.length; j++) {
item2.val = Integer.parseInt(num2[j]);
if (j != num2.length - 1)
item2.next = new ListNode(0);
item2 = item2.next;
}
ListNode rst = addTwoNumbers(l1, l2);
while (rst.next != null) {
System.out.print(rst.val + "->");
rst = rst.next;
}
System.out.println(rst.val);
sc.close();
}
}