[daily algorithm / brush through leetcode] 2 Add two numbers (medium)

Time:2022-5-3

Click here to view more content related to algorithmic interview~

Title Description

Give you two non empty linked lists to represent two non negative integers. They store each number in reverse order, and each node can store only one number.

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

You can assume that neither number starts with 0 except the number 0.

[daily algorithm / brush through leetcode] 2 Add two numbers (medium)

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,1]

Tips:

  • 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

Simple solution (sentry skill)

This is a simulation problem, which simulates the process of manual vertical addition:

Add bit by bit from the lowest bit to the highest bit. If the sum is greater than or equal to 10, keep a digit number and advance 1 to the previous bit at the same time. If there is a carry in the highest bit, fill 1 in the front.

There is a common skill to do the problem of linked list: add a virtual head node (sentry) to help simplify the judgment of boundary conditions.

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(0);
        ListNode tmp = dummy;
        int t = 0;
        while (l1 != null || l2 != null) {
            int a = l1 != null ? l1.val : 0;
            int b = l2 != null ? l2.val : 0;
            t = a + b + t;
            tmp.next = new ListNode(t % 10);
            t /= 10;
            tmp = tmp.next;
            if (l1 != null) l1 = l1.next;
            if (l2 != null) l2 = l2.next;
        }
        if (t > 0) tmp.next = new ListNode(t);
        return dummy.next;
    }
}

Time complexity: m and N represent the length of two linked lists respectively, then the farthest position traversed is $max (m, n) $, and the complexity is $o (max (m, n))$

Space complexity: Max (m, n) + 1 node (including sentry) is created, and the complexity is $o (max (m, n)) $(ignoring constant)

Note: in fact, it is also possible to create $max (M + n) $+ 2 nodes, including the carry of the sentry and the last bit. But the complexity is still $o (max (m, n)) $.


last

This is the third in our “brush through leetcode” seriesNo.2The series began on January 1, 2021. As of the start date, there are 1916 questions on leetcode, some of which are locked questions. We will finish all the unlocked questions first.

In this series of articles, in addition to explaining the problem-solving ideas, we will also give the most concise code as far as possible. If the general explanation is involved, the corresponding code template will also be.

Since leetcode’s questions are increasing with the weekly and biweekly competitions, in order to facilitate us to count the progress, we will calculate the progress according to the total number of questions at the beginning of the series as the denominator and the completed questions as the numerator. Current progress is2/1916

In order to facilitate the students to debug and submit code on the computer, I have established a relevant warehouse: GitHub address & gitee address.

In the warehouse address, you can see the problem solution link of the series of articles, the corresponding code of the series of articles, the original problem link of leetcode and some other preferred problem solutions.

Algorithm and data structure
Leetcode problem solving
Algorithmic interview