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. 第六版 ...
随机推荐
- 数据结构(C语言版)-第8章 排序
8.1 概述 1. 什么是排序? 将一组杂乱无章的数据按一定规律顺次排列起来. 2. 排序的目的是什么? ——便于查找! 3. 什么叫内部排序?什么叫外部排序? 若待排序记录都在内存中,称为内部排序 ...
- Android中SharedPerforences的简单使用示例 --Android基础
SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此activity的状态保存到SharedP ...
- WCF开发框架形成之旅--个人图片信息的上传保存
WCF开发框架形成之旅--个人图片信息的上传保存 http://www.cnblogs.com/wuhuacong/archive/2011/12/23/2299614.html 一般在业务系统里面, ...
- LeetCode--235--二叉树的最近公共祖先
问题描述: 给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x 是 p.q 的 ...
- Practical Node.js (2018版) 第3章:测试/Mocha.js, Chai.js, Expect.js
TDD and BDD for Node.js with Mocha TDD测试驱动开发.自动测试代码. BDD: behavior-driven development行为驱动开发,基于TDD.一种 ...
- canvas学习之粒子动画
项目地址:http://pan.baidu.com/s/1ccTptc 粒子动画意思就是把一个图片粒子画,然后使用粒子作出动画效果,主要两个问题:一个图片如何粒子化,这里面我们使用canvas的get ...
- AutoMatic merge failed;fix conflicts and then commit the result.解决方法
意思是: 冲突内容:合并冲突在 XXXX.DS_Store文件中 自动合并失败:修改冲突然后提交修改后的结果. <<<<<<<< HEAD 你写的代码 ...
- 『Re』正则表达式模块_常用方法记录
『Re』知识工程作业_主体识别 一个比较完备的正则表达式介绍 几个基础函数 re.compile(pattern, flags=0) 将正则表达式模式编译成一个正则表达式对象,它可以用于匹配使用它的m ...
- shiro中JSP标签
Shiro提供了JSTL标签用于在JSP/GSP页面进行权限控制,如根据登录用户显示相应的页面按钮. 导入标签库 <%@taglib prefix="shiro" uri=& ...
- CF-517B-DP
http://codeforces.com/contest/1072/problem/B 给出两个数列 a,b 长度为n-1 ,询问是否能构造出一个长度为n的数列t,使得 所有的ab都满足 a[i] ...