[Leetcode] 2. Add two numbers add the two numbers stored in reverse order of the list

Source: Internet
Author: User

You are given, linked lists representing, and non-negative numbers. The digits is stored in reverse order and all of their nodes contain a single digit. ADD the numbers and return it as a linked list.

Input: (2, 4, 3) + (5, 6, 4)
Output:7, 0, 8

A very natural idea is to go through two linked lists separately, get two addend add1 and ADD2, then get SUM=ADD1+ADD2, and then save the sum with the list in reverse order. This approach ignores the problem that data cannot be stored in int or other integer types when the list length is large. That the problem should be noted is a large number of problems.

Idea: Create a new linked list, and then iterate through the two lists you've entered, each of the two additions, add a new node behind the new list, and pay attention to handling rounding issues. There is the most high-level carry problem to be the last special treatment. Time complexity O (n).

classSolution { Public: ListNode*addtwonumbers (ListNode *l1, ListNode *L2) {ListNode*res =NewListNode (-1); ListNode*cur =Res; intcarry =0;  while(L1 | |L2) {            intN1 = L1? L1->val:0; intN2 = L2? L2->val:0; intsum = n1 + N2 +carry; Carry= SUM/Ten; Cur->next =NewListNode (sum%Ten); Cur= cur->Next; if(l1) L1 = l1->Next; if(L2) L2 = l2->Next; }        if(carry) Cur->next =NewListNode (1); returnRes->Next; }};

On the careercup of the problem there is a follow up, the number of the linked list of the direction of change, the original is the lowest table head, now is the highest position of the table head. It's a little bit troublesome. One idea is that since the numbers are in the opposite direction, we can reverse the two lists separately, and get the same problem as the one above, the same time complexity is O (n):

classSolution { Public: ListNode*addtwonumbers (ListNode *l1, ListNode *L2) {ListNode*dummy =NewListNode (-1); ListNode*cur =dummy; intcarry =0; L1=reverselist (L1); L2=Reverselist (L2);  while(L1 | |L2) {            intN1 = L1? L1->val:0; intN2 = L2? L2->val:0; intsum = n1 + N2 +carry; Carry= SUM/Ten; Cur->next =NewListNode (sum%Ten); Cur= cur->Next; if(l1) L1 = l1->Next; if(L2) L2 = l2->Next; }        if(carry) Cur->next =NewListNode (1); returnReverselist (dummy->next); } ListNode*reverselist (ListNode *head) {        if(!head)returnHead; ListNode*dummy =NewListNode (-1); Dummy->next =Head; ListNode*cur =Head;  while(cur->next) {ListNode*tmp = cur->Next; Cur->next = tmp->Next; TMP->next = dummy->Next; Dummy->next =tmp; }        returnDummy->Next; }};

The second idea is to calculate the length of the two lists separately, and then fill 0 of the short list with the same length as the other list. Because we want to add from the low, and the low is the end of the list, so we use the recursive processing, first traversed to the end of the list, and then add, carry identifier carry with reference, so as to ensure that the recursive return value can be correctly passed, each node after the computation of the back of the node, Until you return to the first node to complete the recursion. Finally, we deal with the rounding problem of the highest bit:

classSolution { Public: ListNode*addtwonumbers (ListNode *l1, ListNode *L2) {        intN1 =0, N2 =0, carry =0;; N1=getlength (L1); N2=GetLength (L2); if(N1 > N2) L2 = padlist (L2, N1-n2); if(N2 > N1) l1 = padlist (L1, N2-N1); ListNode*res =Addtwonumbersdfs (L1, L2, carry); if(Carry = =1) {ListNode*tmp =NewListNode (1); TMP->next =Res; Res=tmp; }        returnRes; } ListNode*addtwonumbersdfs (ListNode *l1, ListNode *l2,int&carry) {        if(!l1 &&!l2)returnNULL; ListNode*list = Addtwonumbersdfs (L1->next, l2->next, carry); intsum = l1->val + L2->val +carry; ListNode*res =NewListNode (sum%Ten); Res->next =list; Carry= SUM/Ten; returnRes; } ListNode*padlist (ListNode *list,intLen) {ListNode*dummy =NewListNode (-1); ListNode*cur =dummy;  for(inti =0; i < Len; ++i) {cur->next =NewListNode (0); Cur= cur->Next; } cur->next =list; returnDummy->Next; }    intGetLength (ListNode *list) {ListNode*cur =list; intres =0;  while(cur) {++Res; Cur= cur->Next; }        returnRes; }};

[Leetcode]2. Add two numbers add the two numbers stored in reverse order of the list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.