Algorithm problem - the sum of two numbers

topic

It is the addition of the numbers represented by the two linked lists, so that the addition of two very large numbers can be realized without considering the limitations of the value int and float.

example

Example 1

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, return [0, 1].

Example 2

Input: nums = [3,2,4], target = 6
Output: [1,2]

Example 3

Input: nums = [3,3], target = 6
Output: [0,1]

train of thought

First of all, the addition of each bit will definitely generate a carry, which we use to represent. The maximum carry will be 1, that is, add the two largest numbers and add the carry, so the maximum is 19, and no carry 2 will be generated. Below are more detailed steps.

  • Initialize a node's head, dummy head, but this head does not store numbers. And point curr to it.
  • Initialize carry to 0.
  • Initialize p and q as the heads of the given two linked lists l1 and l2 respectively, that is, the units bit.
  • Loop until both l1 and l2 reach null.
    Set x to the value of the p node, or set x to 0 if p has reached null.
    Set y to the value of q node, if q has reached null, set y to 0.
    Set sum = x + y + carry.
    Update carry = sum / 10.
    Create a node with value sum mod 10, and point curr's next to it, while curr points to the new node that becomes current.
    Move p and q forward.
  • Determine whether carry is equal to 1, if it is equal to 1, add a node with 1 at the end of the linked list.
  • Returns the next of the dummy head, which is where the single digit starts.

The initialized node dummy head does not store a value, and finally returns the next of the dummy head. The advantage of this is that there is no need to judge and change the value of the head alone. That is, if the head at the beginning represents a single digit, then you don’t know its value at the beginning of initialization, so you need to correct its value separately before entering the loop, and you can’t use only one loop like now concise.

the code


class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    ListNode dummyHead = new ListNode(0);
    ListNode p = l1, q = l2, curr = dummyHead;
    int carry = 0;
    while (p != null || q != null) {
        int x = (p != null) ? p.val : 0;
        int y = (q != null) ? q.val : 0;
        int sum = carry + x + y;
        carry = sum / 10;
        curr.next = new ListNode(sum % 10);
        curr = curr.next;
        if (p != null) p = p.next;
        if (q != null) q = q.next;
    }
    if (carry > 0) {
        curr.next = new ListNode(carry);
    }
    return dummyHead.next;
}

Time complexity: O(max(m,n)), where m and n represent the lengths of l1 and l2.

Space complexity: O(max(m,n)), m and n represent the lengths of l1 and l2. In fact, the maximum length of the new List is O(max(m,n))+1, because our head does not store values.

Summarize

  • In fact, it is to traverse two linked lists at the same time. When traversing, it is necessary to consider whether a linked list has been traversed. The linked list that has been traversed uses 0 as the addend.

  • Since the added value is to be recorded, the sum is recorded in a new linked list while traversing

Guess you like

Origin blog.csdn.net/SO_zxn/article/details/130590452