# The solution and application of the addition of two numbers in three minutes - algorithmic practice

Posted by guitarlvr on Wed, 20 Oct 2021 02:47:30 +0200

• π Personal homepage: IT learning diary
• π¬ If the article is helpful to you, welcome to follow, like, collect (one click, three links) and subscribe to the column
• π To find a small partner for common growth, please click[ Technical circle]

 1, Foreword

## 1, Foreword

in the first two articles, we introduced the derivation logic of time complexity and space complexity. We also officially stepped into the practice of algorithm by solving the problem of "summation of two numbers". As the saying goes, one mountain is more difficult than another. This time, the Boss we met is not simple. If you want to pass the customs, you should turn your little brain and pay attention to the Boss's "trap" all the time.

 2, Topic introduction

## 2, Topic introduction

Boss features: give you two non empty linked lists to represent two non negative integers. Each number is stored in reverse order, and each node can only store one digit.

customs clearance requirements: Please add the two numbers and return a linked list representing sum in the same form.

customs clearance tips: you can assume that neither number will start with 0 except the number 0.

customs clearance example:

```Example 1:
Input: l1 = [2,4,3], l2 = [5,6,4]
Output:[7,0,8]
Explanation: 342 + 465 = 807

Example 2:
Input: l1 = [0], l2 = [0]
Output:[0]

Example 3:
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output:[8,9,9,9,0,0,0,1]
```

customs clearance reminder:

• The number of nodes in each linked list is within the range [1, 100]
• 0 <= Node.val <= 9
• The number represented in the title data guarantee list does not contain leading zeros

 3, Topic interpretation

## 3, Topic interpretation

to solve the Boss, we must first find its weakness. Through the Boss's description, we will find that it has the following characteristics:

• The elements in the linked list are in reverse order. For example, if the value 123 is stored in the linked list, it is 3 - 2 - 1

• Each element in the linked list can only store one digit. If the sum of element values exceeds 10, the Boss will evolve and carry the values to the previous values, and so on. As shown in the above figure, the values of the second element in the two linked lists are 4 and 6 respectively. After they are added, they will get 10. At this time, the Boss will evolve, keep 0 and carry forward 1, so the sum of the third element is 3 + 4 + 1 = 8.

 4, Customs clearance mode 1: human sea tactics

## 4, Customs clearance mode 1: human sea tactics

having mastered the characteristics of the Boss, the first way we can think of is the crowd tactics. No matter how powerful the technology Boss is, we can slowly wear off his blood through a large number of people and finally defeat him. Let's take a look at the specific scheme below:

```public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// The last element of the linked list
ListNode tail = null;
int carry = 0;
// If there are still values in the linked list, add the two numbers (as long as the Boss is still alive, continue to chop after resurrection)
while (l1 != null || l2 != null) {
int l1Val = l1 == null ? 0 : l1.val;
int l2Val = l2 == null ? 0 : l2.val;
// Get the sum of the corresponding elements of the linked list. If it exceeds 10, carry
int sum = l1Val + l2Val + carry;
// Determine whether carry is required
carry = sum / 10;
head = tail = new ListNode(sum % 10);
} else {
tail.next = new ListNode(sum % 10);
// Always point tail to the last element of the linked list for the next round of operation
tail = tail.next;
}
// If there are still values in the linked list, point the element to the next for the remaining calculations
if (l1 != null) {
l1 = l1.next;
}
if (l2 != null) {
l2 = l2.next;
}
}
// If the last element needs carry, point tail to the carry value
if (carry > 0) {
tail.next = new ListNode(carry);
}
}
```

execution results:

sure enough, even if the Boss is strong, he can't stand the scoundrels of the crowd tactics. Finally, the Boss fell at the feet of powerful players. However, at this time, someone complained that it takes so many players to pass the pass. When the number of players is not enough, can there be other schemes that only a few players can pass the pass?

at this time, resourceful Xiaocheng came up with a plan. We can learn from the idea of wheel warfare, divide the designated number of people into different research teams, and constantly grind blood with the Boss. In this way, we can finally pass the customs with the Boss. Everyone agrees with Xiaocheng's idea. Let's see how to realize it.

 5, Customs clearance mode 2: wheel combat (recursive thought)

## 5, Customs clearance mode 2: wheel combat (recursive thought)

```public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// Get the value in the linked list
int l1Val = l1 == null ? 0 : l1.val;
int l2Val = l2 == null ? 0 : l2.val;
// Get the sum of the corresponding elements of the linked list. If it exceeds 10, carry
int sum = l1Val + l2Val;
ListNode node = new ListNode(sum % 10);
int carry = sum / 10;
if (l1.next != null || l2.next != null || carry > 0) {
l1 = l1.next != null ? l1.next : new ListNode(0);
//The new ListCode(0) here is because l1 is finished and l2 is not finished. You have to add the number of l2 and l1. At this time, you can always use l1 to point to a node with a value of 0.
l2 = l2.next != null ? l2.next : new ListNode(0);
l1.val = l1.val + carry;
// Using recursive thought
}
return node;
}
```

execution results:

 6, Sum up experience

## 6, Sum up experience

we can solve the Boss in both of the above two schemes. What is the difference in time complexity between them? Let's deduce their time complexity together!

### 6.1 derivation of time complexity

through the codes of the above two schemes, we will find that with the increase of the linked list elements (the problem scale becomes larger), the number of cycles or recursions we need will also increase. The relationship between the problem scale and the cycle or recursion is f(n) = n. through the derivation of the large o calculation method, we can conclude that the time complexity of the two schemes is O(max(m,n)).

note: max(m,n) means to take the value with the longest length in the two linked lists as the input scale.

### 6.2 application of algorithm ideas in practical business

in the above case, the first scheme uses the most common while loop. This idea is most reflected in ordinary business, such as the comparison and sorting of array or linked list key elements (such as bubbling, selection sorting, etc.).

in the second case, we use the idea of recursion to solve the problem. In ordinary business, the most common use of this idea is to query the menu function of the system. The menu is nested layer by layer, which is just in line with the idea of recursion.

 7, Write at the end

## 7, Write at the end

problems can be poor, but there are endless ways to solve problems. The purpose of brushing leetcode is not only to cope with the interview, but also to cultivate our way of thinking when we encounter and solve problems.

a person can walk fast, but a group of people can go further! In learning, group is the best way. Only by learning from each other and learning from each other can we make faster progress. I sincerely invite you to join the [technology circle] to learn, communicate and grow together!

π [learning materials and interview questions] Click the card below to pay attention and reply to "technical circle" for access π

Topics: Algorithm leetcode recursion