Repository navigation
Expand file tree
/
Copy pathAddTwoNumbers.java
More file actions
83 lines (78 loc) · 2.55 KB
/
Copy pathAddTwoNumbers.java
File metadata and controls
83 lines (78 loc) · 2.55 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
package Novice_class;
/*
* 两个链表相加
* 给定两个链表的头节点head1和head2,
认为从左到右是某个数字从低位到高位,返回相加之后的链表
例子 4 -> 3 -> 6 2 -> 5 -> 3
返回 6 -> 8 -> 9
解释 634 + 352 = 986
* */
public class AddTwoNumbers {
public static class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
public ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
/*
* 思路:1:找到长链表L,短链表S
* 2:划分成3个阶段,阶段1:长链表L,短链表S都有,阶段2:长链表L有,短链表S无,阶段3:长链表L无,短链表S无
* 3:注意进位,不额外开辟空间,直接将相加的结果放到长链表L上。
* */
public static ListNode addTwoNumbers(ListNode head1, ListNode head2) {
// 标注长短链表
ListNode L = listLength(head1) > listLength(head2) ? head1 : head2;
ListNode S = L == head1 ? head2 : head1;
// 标注当前链表的指向节点
ListNode curL = L;
ListNode curS = S;
// 进位
int carry = 0;
// 当前长节点要填的数
int curNuber = 0;
// 一直记录着长链表的尾节点,用于连接第三阶段的节点
ListNode last = L;
// 第一阶段:长链表L,短链表S都有
while(curS != null) {
int curNum = curL.val + curS.val + carry;
carry = curNum / 10;
curNuber = curNum % 10;
curL.val = curNuber;
last = curL;
curL = curL.next;
curS = curS.next;
}
// 第二阶段:长链表L有,短链表S无
while(curL != null) {
int curNum = curL.val + carry;
carry = curNum / 10;
curNuber = curNum % 10;
curL.val = curNuber;
last = curL;
curL = curL.next;
}
// 第三阶段
if(carry == 1) {
ListNode node = new ListNode(1);
last.next = node;
}
return L;
}
// 计算链表的长度
public static int listLength(ListNode head) {
if (head == null) {
return 0;
}
int count = 0;
while(head != null) {
count++;
head = head.next;
}
return count;
}
}