Cracking The Coding Interview2.4
删除前面的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的更多相关文章
- Cracking The Coding Interview2.3
#include <iostream> #include <string> using namespace std; class linklist { private: cla ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the coding interview--问题与解答
http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...
- 《cracking the coding intreview》——链表
前言 最近准备暑假回家回家修整一下,所以时间大部分用来完成项目上的工作,同时为了9月份的校招,晚上的时间我还在学习<cracking the coding intreview>,第二章链表 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
随机推荐
- java调用dll/so文件
大家都知道用C++编写的程序如果用于windows使用则编译为xxx.dll文件,如果是Linux使用则编译为libxxx.so文件.下面将java调用dll/so文件的方法粘出来方便下次使用.此处使 ...
- .bat文件的用途
bat(batch) .bat文件是用来干什么的?我们可以通过win+r,进入cmd,用dos命令行来完成某些操作,比如ping. 我们这里可以用更简单的办法,可以把命令行写入记事本,然后修改后缀为b ...
- English trip V1 - 21. I dreamed dream Teacher:Corrine Key: past tense(过去式)
In this lesson you will learn to describe an experience. 本课将会学习描述一次经历 课上内容(Lesson) 词汇(Key Word ) # ...
- 20171024xlVBA批量获取PPT\WORD\PDF页数
Public Sub ModifyFileNames() Dim FolderPath As String Dim FileNames As Variant Dim dotPos As Long Di ...
- 关于导入高德地图 java.lang.UnsatisfiedLinkError: Couldn't load XXXfrom loader dalvik.system.PathClassLoader[DexPathLis
然后后面就是找不到高德地图提供的地图so 就是上面几个 然后不要忘了在buildGradle文件里添加这么一句话 sourceSets { main { jniLibs.srcDirs = ['lib ...
- day11-15,装饰器
day11 1.装饰器 import time # print(time.time()) # 点数前边是从1970年到现在过了多少秒 # time.sleep(10) # 让程序执行到这里停一会儿 # ...
- 02 爬虫数据解析之re,xpath,beautifulsoup
一.正则匹配 简单用法演示: 字符: print(re.findall(".","abccc31223dn哈哈")) ### . 匹配除了换行符以外的任意字符, ...
- Codeforces 1151F Sonya and Informatics (概率dp)
大意: 给定01序列, 求随机交换k次后, 序列升序的概率. 假设一共$tot$个$0$, 设交换$i$次后前$tot$个数中有$j$个$0$的方案数为$dp[i][j]$, 答案即为$\frac{d ...
- Python Signal 信号
https://blog.csdn.net/kongxx/article/details/50976802 http://blog.itpub.net/7728585/viewspace-214206 ...
- java集合的实现细节--ArrayList和LinkedList
ArrayList和LinkedList的实现差异 List代表一种线性表的数据结构,ArrayList则是一种顺序存储的线性表,ArrayList底层采用动态数组的形式保存每一个集合元素,Link ...