删除前面的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. linux权限管理之文件属性

    文件属性 chattr ======================================================== 文件权限管理之: 文件属性注:设置文件属性(权限),针对所有用 ...

  2. sass制作雪碧图

    1.配置文件config.rb http_path = "../../../" css_dir = "Content/css" sass_dir = " ...

  3. 2-sat学习笔记

    前后缀建图 例:要求n个变量满足至多有1个为true. 暴力:一个点的true向其它n-1个点的false连边,复杂度O(n^2). 正解:prei表示前i个点是否有真. prei的true向prei ...

  4. react中创建组件

    第1种 - 创建组件的方式 > 使用构造函数来创建组件,如果要接收外界传递的数据,需要在 构造函数的参数列表中使用`props`来接收:> 必须要向外return一个合法的JSX创建的虚拟 ...

  5. 【oauth2.0】【2】JAVA 客户端模式

    含义:用户直接向客户端注册,客户端以自己的名义要求"服务提供商"提供服务,其实不存在授权问题 步骤: (A)客户端向认证服务器进行身份认证,并要求一个访问令牌(token). (B ...

  6. Oracle 基本操作--数据类型、修改和删除表、增删改查和复制表

    一.Oracle基础数据类型:数据类型: 创建数据表时,设计数据表的结构问题,也就是设计及确定数据表中各个列的数据类型,是数值.字符.日期还是图像等其他类型. 因为只有设计好数据表结构,系统才会在磁盘 ...

  7. WDA基础十三:常用模板管理

    常用的模板一般是SMW0和OAOR,根据不同需求来的. WAD有个不好的地方就是不支持GUI上的OLE和DOI,所以需要做转换,下面是常用的方式: FUNCTION ZCRM_DOWNLOAD_TEM ...

  8. HDFS shell操作及HDFS Java API编程

    HDFS shell操作及HDFS Java API编程 1.熟悉Hadoop文件结构. 2.进行HDFS shell操作. 3.掌握通过Hadoop Java API对HDFS操作. 4.了解Had ...

  9. 【Python】基础知识

    一.基本概念 1.变量与运算符 Python允许给多个变量同时赋值,等号 (=) 右边的值将赋予左边对应位置的变量. # 将a, b, c的值依次赋予b, c, a b, c, a = a, b, c ...

  10. java高级---->Serializable的过程分析

    本次讲解中我们在上次的基础上,深入的了解一下序列化的流程以及其中的原理.关于序列化的一些知识与使用,请参见我的另一篇博客:java基础---->Serializable的使用.好了,我们进行以下 ...