-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoNumAdd.java
More file actions
102 lines (97 loc) · 3.31 KB
/
Copy pathTwoNumAdd.java
File metadata and controls
102 lines (97 loc) · 3.31 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package AlgorithmTest;
/*
给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。
你可以假设除了数字 0 之外,这两个数字都不会以零开头。
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
* */
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public class TwoNumAdd {
public static void main(String[] args) {
}
public static ListNode Add(ListNode l1, ListNode l2){
int Sum=0; //局部值,如果两数相加大于10 就等于1,如果两数相加小于10就为0
ListNode head1=l1; //第一个链表的头部
ListNode head2=l2; //第二个链表的头部
ListNode result=new ListNode(0); //返回的结果
ListNode resulthead=result;
while(head1!=null && head2!=null){
if (head1.val+head2.val+Sum>=10){
if (Sum==1){
result.next=new ListNode((head1.val+head2.val)+Sum-10);
}else{
result.next=new ListNode((head1.val+head2.val)+Sum-10);
result.next=new ListNode((head1.val+head2.val)+Sum-10);
}
Sum=1;
}else{
if (Sum==1){
result.next=new ListNode(head1.val+head2.val+Sum);
}else{
result.next=new ListNode(head1.val+head2.val);
}
Sum=0;
}
head1=head1.next;
head2=head2.next;
result=result.next;
}
if (head1!=null){
if(Sum==1){
head1.val+=1;
while (head1!=null){
if(head1.val>=10){
result.next=new ListNode(head1.val-10);
result=result.next;
if(head1.next!=null){
head1.next.val+=1;
}else {
result.next=new ListNode(1);
return resulthead.next;
}
head1=head1.next;
}else{
result.next=head1;
break;
}
}
Sum=0;
}else{
result.next=head1;
}
}
if(head2!=null){
if(Sum==1){
head2.val+=1;
while (head2!=null){
if(head2.val>=10){
result.next=new ListNode(head2.val-10);
result=result.next;
if(head2.next!=null){
head2.next.val+=1;
}else {
result.next=new ListNode(1);
return resulthead.next;
}
head2=head2.next;
}else{
result.next=head2;
break;
}
}
Sum=0;
}else{
result.next=head2;
}
}
if(Sum==1){
result.next=new ListNode(1);
}
return resulthead.next;
}
}