Leetcode002 sum of two numbers medium

Time:2020-11-30

Title: give twoNon emptyThe linked list of is used to represent twoNonnegativeThe integer of. Among them, their respective digits are in accordance withReverse orderAnd they can only store one digit per node. If we add these two numbers together, we will return a new linked list to represent their sum. You can assume that except for numbers0In addition, these two numbers will not be0start

Example:

Input: (2 → 4 → 3) + (5 → 6 → 4)
Output: 7 > 0 > 8
Cause: 342 + 465 = 807

Solution:

In the question, it is said that the number of digits is stored in the linked list in reverse order. In fact, this reduces the difficulty because the addition operation happens to start from the single bit, while the chain list starts from the low position from the front to the back, which is just in line with the operation order of the addition (take the data given in the question as an example)

Leetcode002 sum of two numbers medium

Since it is summation, it is not difficult to know. Take out each node in the two linked lists, and then sum (sum). If it is greater than 10, a carry 1 is recorded, and then sum is used to balance 10. The result is the first effective node of the target linked list. The specific process is shown in the following figure:
Leetcode002 sum of two numbers medium

Implementation process:

1. The core method is to traverse the two linked lists, and then sum them one by one

  • Because you do not know the number of nodes in the linked list of the sum of the two requirements (that is, the number of cycles), so select the while loop. As long as any node of the linked list is not null, the loop needs to continue
  • However, because there are carry cases, when the carry is not 0, the loop cannot be stopped (for example, 9999 + 1)

2. Generate a new linked list to store the result of summation

  • For linked list problems, we usually create a flag node, which does not store specific values. Its next is responsible for pointing to the first valid node (always pointing to the first, not moving), mainly to facilitate us to return the target linked list
  • At the same time, we need to create a current node, which is responsible for generating each node. It is mobile. Every time a node is generated, it needs to move a node backward to build a new linked list

The language expression ability is a little poor, the description may not be very clear, there are necessary comments in the code, I hope it can help understand

code implementation

class ListNode
{
    public $val = 0;
    public $next = null;
    public function __construct($val)
    {
        $this->val = $val;
    }
}

class LeetCode002
{
    /**
     * @param ListNode $l1
     * @param ListNode $l2
     * @return ListNode
     */
    public function addTwoNumbers($l1, $l2)
    {
        if ($l1 == null && $l2 == null) {
            return null;
        }

        $tag = new listnode (0); // create an empty node
        $current = $tag;
        $addone = 0; // carry
        while ($l1 != null || $l2 != null || $addOne != 0) {
            $val1 = $l1 == null ? 0 : $l1->val;
            $val2 = $l2 == null ? 0 : $l2->val;
            $sum = $val1 + $val2 + $addOne;
            $current->next = new ListNode($sum % 10);
            $current = $current->next;
            $addone = intval ($sum / 10); // this problem does not exist in strongly typed languages
            if ($l1 != null) {
                $l1 = $l1->next;
            }
            if ($l2 != null) {
                $l2 = $l2->next;
            }
        }
        return $tag->next;
    }
}

//Test Case
$leetCode = new LeetCode002();
$l1 = new ListNode(2);
$l1->next = new ListNode(4);


$l2 = new ListNode(5);
$l2->next = new ListNode(6);

$newList = $leetCode->addTwoNumbers($l1, $l2);
$str = '';
while ($newList != null) {
    $str .= $newList->val.'->';
    $newList = $newList->next;
}

echo $str;

results of enforcement
Leetcode002 sum of two numbers medium