删除前面的linklist,使用node来表示链表
//	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;
struct node
{
int data;
node *next;
}; void init(node *p,int a[], int size)
{
if (a==NULL || size<0)
{
return;
} for (int i = 0; i < size; i++)
{
node *t = new node;
t->data = a[i];
p->next = t;
p = p->next;
}
p->next = NULL;
} void print(node *head)
{
if (!head)
{
return;
} node *p = head->next; while (p)
{
cout<<p->data<<" ";
p = p->next;
}
cout<<endl;
} node * plus(node *a, node *b)
{
node *pa = a->next;
node *pb = b->next;
node *pc = new node;
node *pi = pc;
int i = 0;
int step = 0;
while(pa!=NULL && pb!= NULL)
{
node *t = new node; int temp = pa->data + pb->data +step;
step = 0;
if (temp >=10)
{
temp %= 10;
step = 1;
} t->data =temp;
pi->next = t;
pi = t;
pa = pa->next;
pb = pb->next; } while(pa!= NULL)
{
node *t = new node;
t->data = pa->data + step;
step = 0;
pi->next = t;
pi = t;
pa = pa->next;
} while(pb != NULL)
{
node *t = new node;
t->data = pb->data + step;
step = 0;
pi->next = t;
pi = t;
pb = pa->next;
} if (step>0)
{
node *t = new node;
t->data = step;
pi->next = t;
pi = t;
}
pi->next = NULL; return pc; } int main()
{
int a[6] = {1,4,5,9,7,8};
node *head = new node;
init(head,a,6);
print(head); int b[6] = {1,4,5,9,7,8};
node *h = new node;
init(h,a,6);
print(h); node * r = plus(head, h);
print(r);
return 0;
}

Cracking The Coding Interview2.4的更多相关文章

  1. Cracking The Coding Interview2.3

    #include <iostream> #include <string> using namespace std; class linklist { private: cla ...

  2. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  3. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  4. Cracking the coding interview--问题与解答

    http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...

  5. 《cracking the coding intreview》——链表

    前言 最近准备暑假回家回家修整一下,所以时间大部分用来完成项目上的工作,同时为了9月份的校招,晚上的时间我还在学习<cracking the coding intreview>,第二章链表 ...

  6. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  7. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  8. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  9. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

随机推荐

  1. 接口测试——带token请求post接口(postman学习)

    今天遇到一个接口,是添加备注的,post类型,访问参数中需要带上token才行,我在header 中直接加token参数,接口总返回 403,请登陆 1.考虑yapi接口平台集成的是postman的接 ...

  2. [Spring] 04 Denpendency Injection

    DI Dependency Injection 依赖注入:从程序代码中移除依赖关系的一种设计模式. 这样就可以更容易地管理和测试应用程序. DI使我们的程序编码 loosely coupled.松耦合 ...

  3. delphi 程 序从exe运行改成dll库

    第一种方法: 具体步骤: 1.生成新的或着打开已经存在的工程文件(DPR)(Project1.dpr). 2.选择[View]—>[Project Manager],选中[ProjectGrou ...

  4. vs2013安装及opencv3.0的配置

     vs2013的安装改善计划,不勾选. Windows8  和 windows phone不勾选  然后进行解压安装.(我安装在了e盘的次级目录) 安装完成,点击“启动” 登陆界面,点击“以后再说”. ...

  5. apiCloud 选择图片,选择视频,压缩图片

    选择视频或图片,只需要一个 UIMediaScanner 模板就可以实现,点击链接查看api https://docs.apicloud.com/Client-API/UI-Layout/UIMedi ...

  6. array_unique

    array_unique() 函数移除数组中的重复的值,并返回结果数组. 当几个数组元素的值相等时,只保留第一个元素,其他的元素被删除. 返回的数组中键名不变. <?php $a=array(& ...

  7. XXE漏洞

    原理:XML外部实体注入,简称XXE漏洞,XML数据在传输中数据被修改,服务器执行被恶意插入的代码.当允许引用外部实体时,通过构造恶意内容,就可能导致任意文件读取.系统命令执行.内网端口探测.攻击内网 ...

  8. mysql 8.0.12 修改root密码

    use mysql: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码'; FLUSH PRIVI ...

  9. 从零搭建和配置OSX开发环境

    对于每一名开发者来说,更换系统或者更换电脑的时候,都免不了花上不短的时间去折腾开 发环境的问题.我本人也是三番两次,深知这个过程的繁琐.所有,根据我自己以往的经验, 以及参考一下他人的意见,整理一下关 ...

  10. Git:标签管理

    1.创建标签 1.1切换需要打标签的分支 git checkout branch-name 1.2创建标签 git tag tag-name 1.3查看标签 git tag 1.4注意:打标签时默认在 ...