LeetCode算法题——Add Two Numbers

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;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值