《Cracking the Coding Interview》——第2章:链表——题目5
2014-03-18 02:32
题目:给定两个由单链表表示的数字,返回它们的和。比如(9->9) + (1->2) = 0->2->1,99 + 21 = 120。
解法:逐位相加,注意处理进位、长度不等。
代码:
// 2.5 Given two numbers represented by two lists, write a function that returns sum list. The sum list is list representation of addition of two input numbers.
// Example First List: 5->6->3 // represents number 365
// Second List: 8->4->2 // represents number 248
// Resultant list: 3->1->6 //
// Note :Any Carry forward should also be added as the new node . Any Comments on the code below
#include <cstdio>
using namespace std; struct ListNode {
int val;
ListNode *next;
ListNode(int x): val(x), next(nullptr) {};
}; class Solution {
public:
ListNode* listAddition(ListNode *head1, ListNode *head2) {
if (head1 == nullptr) {
return head2;
} else if (head2 == nullptr) {
return head1;
} int carry = ;
int val;
ListNode *p1, *p2;
ListNode *head, *tail; p1 = head1;
p2 = head2;
head = tail = nullptr;
while (p1 != nullptr && p2 != nullptr) {
val = p1->val + p2->val + carry;
carry = val / ;
val %= ;
if (head == nullptr) {
head = tail = new ListNode(val);
} else {
tail->next = new ListNode(val);
tail = tail->next;
}
p1 = p1->next;
p2 = p2->next;
}
while (p1 != nullptr) {
val = p1->val + carry;
carry = val / ;
val %= ;
tail->next = new ListNode(val);
tail = tail->next;
p1 = p1->next;
}
while (p2 != nullptr) {
val = p2->val + carry;
carry = val / ;
val %= ;
tail->next = new ListNode(val);
tail = tail->next;
p2 = p2->next;
}
if (carry) {
tail->next = new ListNode(carry);
tail = tail->next;
} return head;
}
}; int main()
{
int i;
int n1, n2;
int val;
struct ListNode *head, *head1, *head2, *ptr;
Solution sol; while (scanf("%d%d", &n1, &n2) == && n1 > && n2 > ) {
// create two linked lists
ptr = head1 = nullptr;
for (i = ; i < n1; ++i) {
scanf("%d", &val);
if (head1 == nullptr) {
head1 = ptr = new ListNode(val);
} else {
ptr->next = new ListNode(val);
ptr = ptr->next;
}
}
ptr = head2 = nullptr;
for (i = ; i < n2; ++i) {
scanf("%d", &val);
if (head2 == nullptr) {
head2 = ptr = new ListNode(val);
} else {
ptr->next = new ListNode(val);
ptr = ptr->next;
}
} // add up the two lists
head = sol.listAddition(head1, head2); // print the list
printf("%d", head->val);
ptr = head->next;
while (ptr != nullptr) {
printf("->%d", ptr->val);
ptr = ptr->next;
}
printf("\n"); // delete the list
while (head != nullptr) {
ptr = head->next;
delete head;
head = ptr;
}
while (head1 != nullptr) {
ptr = head1->next;
delete head1;
head1 = ptr;
}
while (head2 != nullptr) {
ptr = head2->next;
delete head2;
head2 = ptr;
}
} return ;
}
《Cracking the Coding Interview》——第2章:链表——题目5的更多相关文章
- Cracking the Coding Interview:: 寻找有环链表的环路起始节点
给定一个有环链表,实现一个算法返回环路的开头节点. 这个问题是由经典面试题-检测链表是否存在环路演变而来.这个问题也是编程之美的判断两个链表是否相交的扩展问题. 首先回顾一下编程之美的问题. 由于如果 ...
- Cracking The Coding Interview 2.0 单链表
#include <iostream> #include <string> using namespace std; class linklist { private: cla ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- 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>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第2章:链表——题目7
2014-03-18 02:57 题目:检查链表是否是回文的,即是否中心对称. 解法:我的做法是将链表从中间对半拆成两条,然后把后半条反转,再与前半条对比.对比完了再将后半条反转了拼回去.这样不涉及额 ...
- 《Cracking the Coding Interview》——第2章:链表——题目6
2014-03-18 02:41 题目:给定一个带有环的单链表,找出环的入口节点. 解法1:用hash来检测重复节点肯定是容易想而且效率也高的好办法. 代码: // 2.6 You have a ci ...
随机推荐
- php 调用微信虚拟支付
摘要:小游戏对接微信虚拟支付,需要自己的后台调用微信的虚拟支付接口,微信的文档实在烂,总结走过的坑. 1.报错{errcode:90007,errmsg:invalid openid} 无效的open ...
- COGS 2091. Asm.Def的打击序列
★★★ 输入文件:asm_lis.in 输出文件:asm_lis.out 简单对比时间限制:4 s 内存限制:256 MB [题目描述] 白色圆柱形的“蓝翔”号在虚空中逐渐变大,一声沉 ...
- 如何使用ABSL代码调用Web service
需求:在C4C UI里创建web service(maintain ticket),然后通过ABSL代码消费. 1. 创建一个新的Communication Arrangement 基于Manage ...
- IOS http协议 总结
HTTP协议1.面试题常见:聊一下HTTP协议(协议的完整的通信过程) ============================================================ 一.一 ...
- EF分组后把查询的字段具体映射到指定类里面的写法
//先做基本查询 var querySql = from l in _logClinicDataOperationRepository.Table select new LogClinicDataOp ...
- Jenkins配置(Jenkins如何与maven项目进行连用)
一同事,在问关于Jenkins搭建后,他是如何与我们项目进行连用的,如何通过Jenkins去编译我们的项目的,现在介绍下如何通过Jenkins持续编译我们的项目 配置过程 1.确定我们已经搭建好了Je ...
- 【CCPC-Wannafly Winter Camp Day4 (Div1) A】夺宝奇兵(水题)
点此看题面 大致题意: 有\(n\)种宝藏,每种各两个.让你依次获得\(1\sim n\)号宝藏,然后依次获得剩余的\(n\sim1\)号宝藏,求最少步数. 简单结论 其实这题有一个十分简单的结论,即 ...
- Codeforces 758B Blown Garland
题目链接:http://codeforces.com/contest/758/problem/B 题意:一个原先为4色环的链子少了部分,要你找出死的最少的一种可能,各输出四种颜色的死了多少. 分析:就 ...
- 轻量级HTTP服务器Nginx(配置与调试Nginx维护Nginx)
轻量级HTTP服务器Nginx(配置与调试Nginx) 文章来源于南非蚂蚁 Nginx安装完毕后,会产生相应的安装目录,根据前面的安装路径,Nginx的配置文件路径为/opt/nginx/conf ...
- Max answer(The Preliminary Contest for ICPC China Nanchang National Invitational)
Alice has a magic array. She suggests that the value of a interval is equal to the sum of the values ...