这是什么新配件?# Joke - 肚皮舞运动
c*g
1 楼
I guess the line 3 and line 4 is not correct.
If both the linked list is NULL but the carry is 1, it should create a
new node and the value is the carry, am I right?
Thanks so much!
2.4 You have two numbers represented by a linked list, where each node
contains a single digit. The digits are stored in reverse order, such
that
the 1’s digit is at the head of the list. Write a function that adds the
two numbers and returns the sum as a linked list.
EXAMPLE
Input: (3 -> 1 -> 5), (5 -> 9 -> 2)
Output: 8 -> 0 -> 8
pg 50
SOLUTION
We can implement this recursively by adding node by node, just as we
would
digit by digit.
1. result.data = (node1 + node2 + any earlier carry) % 10
2. if node1 + node2 > 10, then carry a 1 to the next addition.
3. add the tails of the two nodes, passing along the carry.
1 LinkedListNode addLists(LinkedListNode l1, LinkedListNode l2,
2 int carry) {
3 if (l1 == null && l2 == null) {
4 return null;
5 }
6 LinkedListNode result = new LinkedListNode(carry, null, null);
7 int value = carry;
8 if (l1 != null) {
9 value += l1.data;
10 }
11 if (l2 != null) {
12 value += l2.data;
13 }
14 result.data = value % 10;
15 LinkedListNode more = addLists(l1 == null ? null : l1.next,
16 l2 == null ? null : l2.next,
17 value > 10 ? 1 : 1);
18 result.setNext(more);
19 return result;
20 }
If both the linked list is NULL but the carry is 1, it should create a
new node and the value is the carry, am I right?
Thanks so much!
2.4 You have two numbers represented by a linked list, where each node
contains a single digit. The digits are stored in reverse order, such
that
the 1’s digit is at the head of the list. Write a function that adds the
two numbers and returns the sum as a linked list.
EXAMPLE
Input: (3 -> 1 -> 5), (5 -> 9 -> 2)
Output: 8 -> 0 -> 8
pg 50
SOLUTION
We can implement this recursively by adding node by node, just as we
would
digit by digit.
1. result.data = (node1 + node2 + any earlier carry) % 10
2. if node1 + node2 > 10, then carry a 1 to the next addition.
3. add the tails of the two nodes, passing along the carry.
1 LinkedListNode addLists(LinkedListNode l1, LinkedListNode l2,
2 int carry) {
3 if (l1 == null && l2 == null) {
4 return null;
5 }
6 LinkedListNode result = new LinkedListNode(carry, null, null);
7 int value = carry;
8 if (l1 != null) {
9 value += l1.data;
10 }
11 if (l2 != null) {
12 value += l2.data;
13 }
14 result.data = value % 10;
15 LinkedListNode more = addLists(l1 == null ? null : l1.next,
16 l2 == null ? null : l2.next,
17 value > 10 ? 1 : 1);
18 result.setNext(more);
19 return result;
20 }