-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
79 lines (63 loc) · 1.6 KB
/
Main.java
File metadata and controls
79 lines (63 loc) · 1.6 KB
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
import com.sun.istack.internal.NotNull;
public class Main {
public static void main(String[] args) {
int[] a = {2, 4, 3};
int[] b = {5, 6, 4};
ListNode aListNote = createListNote(a);
ListNode bListNote = createListNote(b);
Solution solution = new Solution();
ListNode result = solution.addTwoNumbers(aListNote, bListNote);
printListNode(aListNote);
printListNode(bListNote);
printListNode(result);
}
private static void printListNode(@NotNull ListNode listNode){
System.out.print("[ ");
while (listNode!=null){
System.out.print(listNode.val+" ");
listNode=listNode.next;
}
System.out.print("]");
System.out.println();
}
private static ListNode createListNote(int[] num) {
ListNode head=null;
ListNode pre=null;
for (int aNum : num) {
ListNode current = new ListNode(aNum);
if (pre == null) {
pre = current;
head = current;
} else {
pre.next = current;
pre = current;
}
}
return head;
}
}
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*
* 难点在于进位和null值的处理
* 进位记录
* null值=0
*
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
return null;
}
}