Two numbers together
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
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/add-two-numbers
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
Official ideas and algorithms same, but in terms of statement might further optimize the
key code:
Result = ListNode new new ListNode ( 0 ); // current node, iterative ListNode head = Result; // head node int Next = 0 ; // if the sum is greater than 10 carry. 1 == Next the while (L1 =! Null | ! | l2 = null ) { int VALUE1, value2; iF (l1 == null ) // when l1 or l2 is empty, assuming the value 0 VALUE1 = 0 ; the 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) //判断是否进位 { next = 0; } else { result.val Result.val = - 10 ; Next = . 1 ; } IF (! L1 = null ) // L1 and l2 is not empty iteration L1 = l1.next; IF (! L2 = null ) l2 = l2.next; IF ( == L1 null && L2 == null ) // simultaneously empty at the end, to avoid erroneous results such statements below BREAK ; result.next = new new ListNode ( 0 ); Result = result.next; } IF (== Next . 1 ) // carry occurs, but l1 and l2 are 0, the next number can not be the circulation, special handling { result.next = new new ListNode ( . 1 ); } return head;
Complete code:
the using the 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 new ListNode ( 0 ); // current node, iterative ListNode head = Result; // head node int Next = 0 ; // if the sum is greater than 10 carry. 1 == Next the while (l1 =! null || l2 =! null ) { int VALUE1, value2; IF (l1 == null ) // when l1 or l2 is empty, assuming the value 0 VALUE1 = 0 ; the else VALUE1 = l1.val; IF (L2 == null ) value2 = 0 ; the else value2 = l2.val; int temp = value1 + value2; result.val = temp + next; if (result.val < 10) //判断是否进位 { next = 0; } else { result.val = result.val - 10; next = 1; } if (l1 != null) // L1 and l2 not empty iteration L1 = l1.next; IF (l2 =! Null ) l2 = l2.next; IF (L1 == null && l2 == null ) // while the end is empty, below to avoid such statements result of the error BREAK ; result.next = new new ListNode ( 0 ); result = result.next; } IF (== Next . 1 ) //Carry occurs, but l1 and l2 are 0, the next number can not be the circulation, special handling { result.next = new new ListNode ( . 1 ); } return head; } static void the Main ( String [] args) // simple test { ListNode L1 = new new ListNode ( . 9 ); l1.next = new new ListNode ( . 9 ); // l1.next.next ListNode new new = (. 3); ListNode L2 = new 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
A start from the lowest sum, the carry process is greater than 9, up to the last process is completed
Pay particular attention to the following special circumstances:
Test Case | Explanation |
---|---|
l1 = [0,1], l2 = [0,1,2] l2 = [0,1,2] | When a longer list than another list |
l1 = [], l2 = [0,1] l2 = [0,1] | When a list is empty, empty list appears |
l1 = [9,9], l2 = [1] l2 = [1] | Finally, an additional summation carry possible, it is very easy to forget |