ADD Numbers Leetcode

Source: Internet
Author: User
Tags add numbers

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

The title means to think of two linked lists as two numbers, adding up, similar to integers, but now each one is represented by a chain node, noting that the previous number is low

Ideas:

Define two pointers p,q walk through two pointers, add each bit to the previous digit, and, because you don't know that the chain ends first, give the node of the two chain the latest value

Update the value of carry, save p and Q and then continue traversing P=p->next,q=q->next

according to The idea of merging ordered chain list , add the remaining chain nodes to the rounding note that if the chain ends with rounding, you need a new definition node (which is why you want to save p and Q, and then P=p->next,q=q->next traversing down )

The code is as follows:

<span style= "FONT-SIZE:18PX;" >/** * Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x): Val (x), Next (NULL) {} *};            */class Solution {Public:listnode *addtwonumbers (ListNode *l1, ListNode *l2) {if (l1==null)        return L2;        if (l2==null) return L1;        ListNode *p=l1;                ListNode *q=l2; ListNode *prep=null,*preq=null;//defines two pointers used to point to the p,q of the previous int temp;//used to hold the current and int carry=0;//to denote carry Whil            E (p&&q) {prep=p;            preq=q;            Temp= (P->val+q->val+carry);  p->val=temp%10;            Because I do not know the end of the first, so we have to change the value of the two linked list q->val=temp%10;                        CARRY=TEMP/10;            p=p->next;        q=q->next; } if (P==null&&q==null)//Indicates the same number of links, P,q is all over {if (carry>0)//Indicates a new node is required if there is a carry-over after the end, notice that the new section              Point, the next node of the node to point to null {  ListNode *endp=new ListNode (carry);       prep->next=endp;            or Preq. endp->next=null;        } return L1; if (p) {//if p is not finished while (p)//Then P is left plus carry {temp=p->                Val+carry;                p->val=temp%10;                CARRY=TEMP/10;                Prep=p;            p=p->next; } if (carry>0)//means p ends with rounding, you need to create a new node after p {listnode *endp=new ListNode (car                    RY);                    prep->next=endp;                               endp->next=null;        } return L1;            } while (q)//Executes here, indicating that none of the above is returned, then the Q is not finished adding carry {temp=q->val+carry;            q->val=temp%10;            CARRY=TEMP/10;            preq=q;        q=q->next; } if (carry>0)//above indicates that there is a rounding after Q and a new node {ListNode *endq=n is required.EW ListNode (carry);            preq->next=endq;        endq->next=null;            } return L2; }};</span>


ADD Numbers Leetcode

Related Article

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.