LC.P445[两数相加II]

方法一:栈+迭代

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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
Deque<Integer> s1 = new ArrayDeque<>(), s2 = new ArrayDeque<>();
while (l1 != null) {
s1.push(l1.val);
l1 = l1.next;
}
while (l2 != null) {
s2.push(l2.val);
l2 = l2.next;
}
ListNode head = new ListNode();
int c = 0;
while (!s1.isEmpty() || !s2.isEmpty()) {
if (!s1.isEmpty()) c += s1.pop();
if (!s2.isEmpty()) c += s2.pop();
head.next = new ListNode(c % 10, head.next);
c /= 10;
}
if (c > 0) {
head.next = new ListNode(c, head.next);
}
return head.next;
}
}
  • 时间复杂度:$O(max(m,n))$,其中$m$为$l1$的长度,$n$为$l2$的长度
  • 空间复杂度:$O(m+n)$

方法二:反转链表(迭代)+迭代

  1. 将$l1$反转
  2. 将$l2$反转
  3. 调用LC.P2[两数相加],得到$l3$
  4. 反转链表$l3$,得到答案
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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
l1 = reverse(l1);
l2 = reverse(l2);
ListNode l3 = addTwo(l1, l2);
return reverse(l3);
}

private ListNode addTwo(ListNode l1, ListNode l2) {
ListNode head = new ListNode(), tail = head;
int c = 0;
while (l1 != null || l2 != null) {
if (l1 != null) c += l1.val;
if (l2 != null) c += l2.val;
tail.next = new ListNode(c % 10);
tail = tail.next;
c /= 10;
if (l1 != null) l1 = l1.next;
if (l2 != null) l2 = l2.next;
}
if (c > 0) {
tail.next = new ListNode(c);
}
return head.next;
}

private ListNode reverse(ListNode head) {
ListNode cur = head, pre = null;
while (cur != null) {
ListNode temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
  • 时间复杂度:$O(max(m,n))$,其中$m$为$l1$的长度,$n$为$l2$的长度
  • 空间复杂度:$O(1)$

方法三:反转链表(递归)+迭代

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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
l1 = reverse(l1, null);
l2 = reverse(l2, null);
ListNode l3 = addTwo(l1, l2);
return reverse(l3, null);
}

private ListNode addTwo(ListNode l1, ListNode l2) {
ListNode head = new ListNode(), tail = head;
int c = 0;
while (l1 != null || l2 != null) {
if (l1 != null) c += l1.val;
if (l2 != null) c += l2.val;
tail.next = new ListNode(c % 10);
tail = tail.next;
c /= 10;
if (l1 != null) l1 = l1.next;
if (l2 != null) l2 = l2.next;
}
if (c > 0) {
tail.next = new ListNode(c);
}
return head.next;
}

private ListNode reverse(ListNode cur, ListNode pre) {
if (cur == null) return pre;
ListNode head = reverse(cur.next, cur);
cur.next = pre;
return head;
}
}
  • 时间复杂度:$O(max(m,n))$,其中$m$为$l1$的长度,$n$为$l2$的长度
  • 空间复杂度:$O(max(m,n))$