Daily brushing title 191 120

A slag slag Bloggers, brush leetcode Chou Chou himself, the great God who made better way to look to the wing. Title and solution from power button (LeetCode), Portal .

algorithm:

We are given two non-empty list is used to represent two non-negative integer. Where their respective bits are stored in reverse order of the way, and they each node can store only one digit.

If we add up these two numbers, it will return a new list and to represent them.

You can assume that in addition to the numbers 0, these two numbers will not begin with 0.

Example:

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

  The beginning of a very simple idea, in fact, the numbers on the sum of the corresponding position, if there is a carry it again next position +1, in fact, ordinary addition.

public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
        {
            was value = (l1.val + l2.val)% 10 ;
            were flag = (l1.val + l2.val) / 10 > 0 ;

            var result = new ListNode(value);

            was returnResult = result;

            if (flag)
            {
                result.next = new ListNode(1);
            }
            
            There are a = l1.next;
            There are b = l2.next;

            while (a != null || b != null)
            {
                var temp = (a != null ? a.val : 0)
                    + (b != null ? b.val : 0)
                    + (result.next != null ? result.next.val : 0);

                was addNums temp% = 10 ;
                was addFlag = temp / 10 > 0 ;

                if (result.next == null)
                {
                    result.next = new ListNode(addNums);
                }
                else
                {
                    result.next.val = addNums;
                }

                a = a!=null ? a.next : null;
                b = b!=null ? b.next : null;
                result = result.next;

                if (addFlag)
                {
                    result.next = new ListNode(1);
                }
            }

            return returnResult;
        }

Second, the slightly tidied, the first judgment eliminates. Be careful here about the problems pointed, ListNode is a reference type, in fact, points to the existence of heap memory address. So the beginning of the result with the same address returnResult actually pointing. Such is next assignment to be careful, if given a start value is null, will lead to cut off the list.

public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
        {
            ListNode result = new ListNode(0);
            var returnResult = result;

            There are a = l1;
            There are b = l2;

            while (a != null || b != null)
            {
                var temp = (a != null ? a.val : 0)
                    + (b != null ? b.val : 0)
                    + (result.next != null ? result.next.val : 0);

                was addNums temp% = 10 ;
                was addFlag = temp / 10 > 0 ;

                result.next = new ListNode(addNums);
                result = result.next;

                a = a!=null ? a.next : null;
                b = b!=null ? b.next : null;

                if (addFlag)
                {
                    result.next = new ListNode(1);
                }
            }

            return returnResult.next;
        }

 

Guess you like

Origin www.cnblogs.com/dogtwo0214/p/11899528.html