CareerCup-2.4

简介:

You have two numbers represented by a linked list, where each node contains a single digit The digits are stored in reverse order, such that the 1’s digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.

EXAMPLE Input: (3 -> 1 -> 5) + (5 -> 9 -> 2)

Output: 8 -> 0 -> 8

我采用非递归,按照答案的思路写了递归的方法

#include <iostream>
using namespace std;

// Creating a Linked List:
struct Node
{
    Node(int d):data(d){next = NULL;};
    int data;
    Node* next;
};

void printNode(Node* head)
{
    cout<<"Print:"<<endl;
    while(head != NULL)
    {
        cout<<head->data<<endl;
        head = head->next;
    }
    cout<<"End"<<endl;
};

Node* plusNode(Node *o1, Node *o2)
{
    if(o1 == NULL || o2 == NULL)
        return NULL;
    Node *head = new Node(0);
    Node *p = head, *q;
    int temp, flag = 0;
    while(o1 != NULL || o2 != NULL)
    {
        temp = (o1==NULL?0:o1->data) + (o2==NULL?0:o2->data) + flag;
        flag = temp / 10;
        q = new Node(temp % 10);
        p->next = q;
        p = p->next;
        if(o1 != NULL)
            o1 = o1->next;
        if(o2 != NULL)
            o2 = o2->next;
    }
    p->next = NULL;
    p = head->next;
    delete head;
    return p;  
};

Node* plusNodeRec(Node *o1, Node *o2, int carry)
{
    if(o1 == NULL && o2 == NULL)
        return NULL;
    int result = (o1==NULL?0:o1->data) + (o2==NULL?0:o2->data) + carry;
    Node* p = new Node(result % 10);
    Node* next = plusNodeRec(o1==NULL?NULL:o1->next, 
            o2==NULL?NULL:o2->next, result / 10);
    p->next = next;
    return p;
};

int main()
{
    Node* head1 = new Node(3);
    head1->next = new Node(1);
    Node* head2 = new Node(5);
    head2->next = new Node(9);
    head2->next->next = new Node(2);
    // printNode(plusNode(head1, head2));
    printNode(plusNodeRec(head1, head2, 0));
    system("pause"); 
};

目录
相关文章
|
安全 API 数据安全/隐私保护
API接口知识小结
应用程序接口API(Application Programming Interface),是提供特定业务输出能力、连接不同系统的一种约定。这里包括外部系统与提供服务的系统(中后台系统)或后台不同系统之间的交互点。包括外部接口、内部接口,内部接口又包括:上层服务与下层服务接口、同级接口。
|
存储 分布式计算 数据库
阿里云国际版设置数据库云分析工作负载的 ClickHouse 版
阿里云国际版设置数据库云分析工作负载的 ClickHouse 版
|
11月前
|
XML 移动开发 数据库
XML 编辑器
XML 编辑器
|
监控 前端开发 关系型数据库
|
分布式计算 Prometheus 资源调度
分布式计算引擎 Flink/Spark on k8s 的实现对比以及实践
以 Flink 和 Spark 为代表的分布式流批计算框架的下层资源管理平台逐渐从 Hadoop 生态的 YARN 转向 Kubernetes 生态的 k8s 原生 scheduler 以及周边资源调度器,比如 Volcano 和 Yunikorn 等。这篇文章简单比较一下两种计算框架在 Native Kubernetes 的支持和实现上的异同,以及对于应用到生产环境我们还需要做些什么。
771 54
分布式计算引擎 Flink/Spark on k8s 的实现对比以及实践
|
机器学习/深度学习 人工智能 算法
|
机器学习/深度学习 人工智能 算法
【Pytorch神经网络理论篇】 23 对抗神经网络:概述流程 + WGAN模型 + WGAN-gp模型 + 条件GAN + WGAN-div + W散度
GAN的原理与条件变分自编码神经网络的原理一样。这种做法可以理解为给GAN增加一个条件,让网络学习图片分布时加入标签因素,这样可以按照标签的数值来生成指定的图片。
1252 0
|
缓存 NoSQL Redis
秒杀服务------技术点及亮点
秒杀服务------技术点及亮点
178 0
C#编程-51:窗体CancelButton的使用
C#编程-51:窗体CancelButton的使用
249 0
|
分布式计算 大数据 数据库
佰腾科技的专利大数据的云上裂变之路
在票选最美云上大数据暨大数据技术峰会上,来自江苏佰腾科技有限公司的许鹏通过介绍佰腾专利大数据平台的演化、上云前后的平台结构和任务处理流程,为大家分享了专利大数据的云上裂变之路,解释了非专业人士也能进行专利信息的检索与统计,即专利信息的大众化。
6236 0