LeetCode算法题——Add Two Numbers

    xiaoxiao2021-03-25  75

    Description: You are given two  non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

    You may assume the two numbers do not contain any leading zero, except the number 0 itself.

    Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 算法思想:创建链表,逐位相加,逢十进一,保存进位,加到下一位中运算 C++代码如下: #include <iostream> #include "stdlib.h" using namespace std; struct ListNode {       int val;       ListNode *next;       ListNode(int x) : val(x), next(NULL) {} }; class Solution { public:     ListNode* addTwoNumbers(ListNode *l1, ListNode *l2) {         int v1,v2,j=0,v;         ListNode *head,*p1,*p2;         head=p1=NULL;         while(l1!=NULL||l2!=NULL){             if(l1!=NULL){                 v1=l1->val;                 l1=l1->next;             }else{                 v1=0;             }             if(l2!=NULL){                 v2=l2->val;                 l2=l2->next;             }else{                 v2=0;             }             if(v1+v2+j<10){                 v=v1+v2+j;                 j=0;             }else{                 v=v1+v2+j-10;                 j=1;             }             p2=(ListNode *)malloc(sizeof(ListNode));             p2->val=v;             p2->next=NULL;             if(head==NULL){                 head=p1=p2;             }else{                 p1->next=p2;                 p1=p1->next;             }         }         if(j==1){             p2=(ListNode *)malloc(sizeof(ListNode));             p2->val=1;             p2->next=NULL;                if(head==NULL){                 head=p1=p2;             }else{                 p1->next=p2;                 p1=p1->next;             }         }         return head;     } }; int main(){     ListNode *l1h,*p1,*p2,*l2h,*q1,*q2,*res;     p1=NULL;     l1h=NULL;     q1=NULL;     l2h=NULL;     int n,t,m,s;     cout<<"please input the first List element number:";     cin>>n;     for(int i=0;i<n;i++){         p2=(ListNode *)malloc(sizeof(ListNode));         cin>>t;         p2->val=t;         p2->next=NULL;         if(l1h==NULL){             l1h=p1=p2;         }else{             p1->next=p2;             p1=p1->next;         }                    }     cout<<"please input the second List element number:";     cin>>m;     for(int i=0;i<m;i++){         q2=(ListNode *)malloc(sizeof(ListNode));         cin>>s;         q2->val=s;         q2->next=NULL;         if(l2h==NULL){             l2h=q1=q2;         }else{             q1->next=q2;             q1=q1->next;         }                    } //    while(l1h!=NULL){ //        cout<<l1h->val<<" "; //        l1h=l1h->next; //    } //    cout<<endl; //    while(l2h!=NULL){ //        cout<<l2h->val<<" "; //        l2h=l2h->next; //    }     Solution sol;     res=sol.addTwoNumbers(l1h,l2h);     while(res!=NULL){         cout<<res->val<<" ";         res=res->next;     } }
    转载请注明原文地址: https://ju.6miu.com/read-16499.html

    最新回复(0)