C++ 链式A+B

35 篇文章 0 订阅
#include<iostream>
using namespace std;

/**
有两个用链表表示的整数,每个结点包含一个数位。
这些数位是反向存放的,也就是个位排在链表的首部。
编写函数对这两个整数求和,并用链表形式返回结果。
给定两个链表ListNode* A,ListNode* B,请返回A+B的结果(ListNode*)

算法:
使用两个指针分别指向链表A和链表B的首部,并分别将对应的当前元素相加

**/

struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
class Plus {
public:
    ListNode* plusAB(ListNode* a, ListNode* b) {
        // write code here

        //cout<<"enter the function"<<endl;

        if(a==NULL){
            return b;
        }
        if(b==NULL){
            return a;
        }
        ListNode *out_head=new ListNode(0);
        ListNode *out=out_head;
        bool flag=false;
        while(a!=NULL and b!=NULL){
            int temp=a->val+b->val;
            if(flag==true){
                temp++;
            }
            if(temp>=10){
                flag=true;
                temp=temp%10;
            }
            else{
                flag=false;
            }

            //cout<<temp<<"temp"<<endl;

            ListNode *new_node=new ListNode(temp);
            out->next=new_node;
            out=out->next;

            a=a->next;
            b=b->next;

        }

        //cout<<"outside while loop"<<endl;

        ListNode *extend;
        if(a!=NULL){
            extend=a;
        }
        if(b!=NULL){
            //cout<<"extend"<<endl;
            extend=b;
        }
        if(flag==false){
            out->next=extend;
        }
        else{
            // flag=true,need to be 进位
            while(extend!=NULL){
                int temp=extend->val+1;
                if(temp>=10){
                    flag=true;
                    temp=temp%10;
                }
                else{
                    flag=false;
                }
                ListNode *new_node=new ListNode(temp);
                out->next=new_node;
                out=out->next;

                extend=extend->next;
            }
            if(flag==true){
                ListNode *new_node=new ListNode(1);
                out->next=new_node;
            }
        }
        return out_head->next;
    }
};

int main(){
    int array_a[3]={1,2,3};
    ListNode *head_a=new ListNode(0);
    ListNode *a=head_a;

    int array_b[7]={3,2,9,9,9,9,2};
    ListNode *head_b=new ListNode(0);
    ListNode *b=head_b;

    for(int i=0;i<3;i++){
        ListNode *new_node=new ListNode(array_a[i]);
        a->next=new_node;
        a=a->next;
    }

    for(int j=0;j<7;j++){
        ListNode *new_node=new ListNode(array_b[j]);
        b->next=new_node;
        b=b->next;
    }
    Plus solution;
    ListNode *out;
    out=solution.plusAB(head_a->next,head_b->next);

    while(out!=NULL){
        cout<<"output"<<out->val<<endl;
        out=out->next;
    }

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值