Addition of Two Numbers (C# Data Structure and Algorithmic Exercise)

Keywords: C# network

Addition of two numbers

Two non-empty linked lists are given to represent two non-negative integers. Among them, their respective digits are stored in reverse order, and each of their nodes can only store one digit.

If we add the two numbers together, we return a new list to represent their sum.

You can assume that neither of these numbers will begin with zero except the number 0.

Example:

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

Source: LeetCode
Link: https://leetcode-cn.com/problems/add-two-numbers
Copyright belongs to the seizure network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

The idea of the algorithm is the same as that officially, but in terms of sentences, it may be further optimized.
Key code:

            ListNode result= new ListNode(0);      //Current node, iterate
            ListNode head = result;                //Header node
            int next = 0;                          //If it is increased to 10 digits next==1
            while (l1 != null || l2 != null)
            {
                int value1, value2;
                if (l1 == null)                   //When l1 or l2 For space-time, the hypothesis is 0
                    value1 = 0;
                else
                    value1 = l1.val;
                if (l2 == null)
                    value2 = 0;
                else
                    value2 = l2.val;
                int temp = value1 + value2;
                result.val = temp + next;
                if (result.val < 10)             //Judging whether to move in or not
                {
                    next = 0;
                }
                else
                {
                    result.val = result.val - 10;
                    next = 1;
                }
                if (l1 != null)                   //l1 and l2 Not Space-Time Iteration
                    l1 = l1.next;
                if (l2 != null)
                    l2 = l2.next;
                if (l1 == null && l2 == null)     //At the same time, it ends in space-time to avoid errors caused by the following statements
                    break;
                result.next = new ListNode(0);
                result = result.next;
            }
            if (next == 1)                        //Carry-in but l1 and l2 When the next number is 0, the above cycle cannot be carried out. Do special treatment.
            {
                result.next = new ListNode(1);
            }
            return head;

 

Complete code:

using System;
namespace numAdd
{
    public class ListNode
    {
    public int val;
    public ListNode next;
    public ListNode(int x) { val = x; }
    }
    class Program
    {
        public static ListNode AddTwonumbers(ListNode l1, ListNode l2)
        {
            ListNode result= new ListNode(0);      //Current node, iterate
            ListNode head = result;                //Header node
            int next = 0;                          //If it is increased to 10 digits next==1
            while (l1 != null || l2 != null)
            {
                int value1, value2;
                if (l1 == null)                   //When l1 or l2 For space-time, the hypothesis is 0
                    value1 = 0;
                else
                    value1 = l1.val;
                if (l2 == null)
                    value2 = 0;
                else
                    value2 = l2.val;
                int temp = value1 + value2;
                result.val = temp + next;
                if (result.val < 10)             //Judging whether to move in or not
                {
                    next = 0;
                }
                else
                {
                    result.val = result.val - 10;
                    next = 1;
                }
                if (l1 != null)                   //l1 and l2 Not Space-Time Iteration
                    l1 = l1.next;
                if (l2 != null)
                    l2 = l2.next;
                if (l1 == null && l2 == null)     //At the same time, it ends in space-time to avoid errors caused by the following statements
                    break;
                result.next = new ListNode(0);
                result = result.next;
            }
            if (next == 1)                        //Carry-in but l1 and l2 When the next number is zero, the above cycle can not be carried out and special processing is done.
            {
                result.next = new ListNode(1);
            }
            return head;
        }
        static void Main(string[] args)          //Simple test
        {
            ListNode l1 = new ListNode(9);
            l1.next = new ListNode(9);
            //l1.next.next = new ListNode(3);

            ListNode l2 = new ListNode(1);
            //l1.next = new ListNode(6);
            //l1.next.next = new ListNode(4);

            ListNode l3 = AddTwonumbers(l1, l2);
            while (l3 != null)
            {
                Console.WriteLine(l3.val);
                l3 = l3.next;
            }
        }
    }
}

algorithm

From the lowest bit to add up, carry processing is greater than 9, until the final processing is completed.

Special attention should be paid to the following special situations:

test case Explain
l1=[0,1],l2=[0,1,2]l2=[0,1,2] When one list is longer than another
l1=[],l2=[0,1]l2=[0,1] When a list is empty, an empty list appears.
l1=[9,9],l2=[1]l2=[1] It's easy to forget that summation operations may end up with extra carry.

Posted by waygood on Sun, 13 Oct 2019 07:01:55 -0700