《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 ...
随机推荐
- 51nod 1366 贫富差距
题目来源: TopCoder 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 一个国家有N个公民,标记为0,1,2,...,N-1,每个公民有一个存款额.已知每个公 ...
- zendstudio 汉化
http://archive.eclipse.org/technology/babel/index.php http://www.eclipse.org/babel/downloads.php 注册码 ...
- 利用Js或Css滤镜实现IE6中PNG图片半透明效果 IE6PNG妥妥的
接下来介绍几种PNG图片在IE6中不透明的解决办法 1.用自己的PNG,让IE6一边去吧 首先制作PNG图片的时候,另存为一个GIF图片,因为IE6是支持GIF图片透明,然后在css定义 .pngte ...
- IOS 制作动画代码和 设置控件透明度
方式1: //animateWithDuration用1秒钟的时间,执行代码 [UIView animateWithDuration:1.0 animations:^{ //存放需要执行的动画代码 s ...
- 将一个命令的输出保存到CSV文件
执行段: 结果段: 补充:配合不同的命令可以使工作更加简单 使用Imort-Csv命令从文件中导入结构化数据
- 2018.8.2 Juint测试介绍及其命名的规范
JUnit - 测试框架 什么是 Junit 测试框架? JUnit 是一个回归测试框架,被开发者用于实施对应用程序的单元测试,加快程序编制速度,同时提高编码的质量.JUnit 测试框架能够轻松完成以 ...
- Java的感受
感觉Java很重要,但是学起来好像并不比C语言简单.
- test image
Most of these images are in PBM or PGM format and compressed with GNU Zip and GNU TAR Note: These pa ...
- a=a+(a++);b=b+(++b);计算顺序,反汇编
a=a+(a++); 013913BC mov eax,dword ptr [a] 013913BF add eax,dword ptr [a] 013913C2 mov dword ptr [a], ...
- Oracle多表连接方法
笛卡尔连接[结果集为各表记录的乘积] SELECTt * FROM table_1, table_2, table_n SELECTt * FROM table_1 CROSS JOIN table_ ...