Preface explains

Algorithm learning, daily brush record.

Subject to connect

The two together

The subject content

You are given two non-empty linked lists representing two non-negative integers. Each digit is stored in reverse order, and only one digit can be stored per node.

You add the two numbers and return a linked list representing the sum in the same form.

You can assume that neither of these numbers will start with 0, except for the number 0.

Example 1:

Input: l1 = [2,4,3], l2 = [5,6,4]

Output:,0,8 [7]

Explanation: 342 + 465 = 807

Example 2:

Input: L1 = [0], L2 = [0]

Output: [0]

Example 3:

Input: l1 = [9,9,9,9,9,9], l2 = [9,9,9,9]

Output:,9,9,9,0,0,0,1 [8]

Tip:

The number of nodes in each linked list is in the range [1, 100]

0 <= Node.val <= 9

The question data ensures that the numbers represented in the list do not contain leading zeros

The analysis process

If you convert the values of two linked lists, add them together to get a new value, and then convert the new value to a linked list, this is not feasible.

Because the range is too large, the number of nodes in each linked list is in the range [1, 100], and the maximum number has reached 100 digits, which may overflow when the linked list is converted to values, resulting in incorrect values of the transformation.

This problem needs to use the method of simulation, in accordance with our usual vertical calculation of the addition of two numbers, while traversing two linked lists from beginning to end, to construct the linked list of the sum of two numbers.

The first step

Defines the middle node of a list that computes the sum of two numbers.

Define a linked list of the sum of two numbers, head, which initially points to node and returns head directly at the end.

Defines whether carry isCarry, initially false, is needed to handle carry during vertical computation.

The second step

While loop, simulating vertical calculation, traverses two linked lists L1 and L2 from beginning to end at the same time to construct a linked list of the sum of two numbers. When both lists L1 and L2 are empty, the loop ends.

The third step

For each while loop:

1. Define val as the result of the sum of two numbers in vertical calculation, process two linked lists L1 and L2 respectively, assign the value to val first, and then add the pointer to next node next.

2. Determine whether there is a carry by isCarry. If there is a carry, val is added by 1.

3. Update isCarry by checking whether val is greater than 9.

4, if val is greater than 9, val minus 10 takes the units digit.

5. Assign val to the value of node.

If both lists l1 and L2 are not empty, there will be more nodes in the list. Otherwise, there will be no nodes in the list. When constructing the next node, the next pointer of the middle node node next points to a newly constructed node, and the middle indirect node node itself points to its next node node.next. The next loop assigns the next value to the next node.

The fourth step

At the end of the while loop, if there is still a carry, the previously constructed list is added with a new node. That is, the next node in the middle is added to the last node of the list. The next node pointer to node next points to a newly constructed node with a value of 1.

Step 5

End the program by returning the linked list head of the sum of two numbers.

To solve the code

/** * 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) { ListNode node = new ListNode(); ListNode = new ListNode(); ListNode head = node; Boolean isCarry = false; While (l1!) (l1!) (l1!) (l1!) = null || l2 ! Int val = 0; int val = 0; If (l1! = null) { val += l1.val; l1 = l1.next; } // Process the second list if (l2! = null) { val += l2.val; l2 = l2.next; } val = isCarry? val + 1 : val; // Check whether the current bit is carried. If it is greater than 9, carry isCarry = val > 9; If (val > 9) {// If (val > 9); } // Assign the result of the addition of the two numbers to the node of the new list. if (l1 ! = null || l2 ! Node.next = new ListNode(); // If both lists are empty, there will be no more nodes. node = node.next; }} if (isCarry) {next = new ListNode(1);}} if (isCarry) {next = new ListNode(1); } // return a list of the sum of two numbers. }}Copy the code

Submit the results

The execution time was 2ms, beating 96.94% of users in time, 38.7MB in memory consumption, and 52.44% in space.

The original link

Add two numbers