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的更多相关文章

  1. Cracking the Coding Interview:: 寻找有环链表的环路起始节点

    给定一个有环链表,实现一个算法返回环路的开头节点. 这个问题是由经典面试题-检测链表是否存在环路演变而来.这个问题也是编程之美的判断两个链表是否相交的扩展问题. 首先回顾一下编程之美的问题. 由于如果 ...

  2. Cracking The Coding Interview 2.0 单链表

    #include <iostream> #include <string> using namespace std; class linklist { private: cla ...

  3. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  4. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  5. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  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>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  9. 《Cracking the Coding Interview》——第2章:链表——题目7

    2014-03-18 02:57 题目:检查链表是否是回文的,即是否中心对称. 解法:我的做法是将链表从中间对半拆成两条,然后把后半条反转,再与前半条对比.对比完了再将后半条反转了拼回去.这样不涉及额 ...

  10. 《Cracking the Coding Interview》——第2章:链表——题目6

    2014-03-18 02:41 题目:给定一个带有环的单链表,找出环的入口节点. 解法1:用hash来检测重复节点肯定是容易想而且效率也高的好办法. 代码: // 2.6 You have a ci ...

随机推荐

  1. react+webpack 引入字体图标

    在使用react+webpack 构建项目过程中免不了要用到字体图标,在引入过程中报错,不能识别字体图标文件中的@符,报错 Uncaught Error: Module parse failed: U ...

  2. do..while(false)的用法总结

    首先要注意: do..while(0) 代表do里面的东西至少被执行一次,在这里仅仅执行一次. 此种用法有三个用处: 代替{}代码块,实现局部作用域.在某些宏定义时非常有用: #define f(x) ...

  3. 修改hosts工具推荐SwitchHosts

    推荐一个修改hosts的工具.适合平时工作中经常修改hosts的开发测试. 下载地址:https://oldj.github.io/SwitchHosts/ 可以按各种环境或者项目添加,用的时候打开或 ...

  4. Apache服务器开启gzip压缩的支持

    为什么要在服务器上开启压缩?其实,服务器上开启压缩,对整个网站的就是在服务器上把网页的内容压缩后传给客户端,客户端解压后再显示网页的内容.实际就是增加了服务器端和客户端的工作量,减少了网络传输的数据量 ...

  5. powerdesigner15 反向工程

  6. Python 初始—(列表)

    列表切片 数组data=[a,b,c,d,e] print(data[1,3])#取出b,c , 如果用-号切片则是反向取数,那么去取出来的数为data[-3,-1],如果是0则默认不填 列表追加 d ...

  7. html5 canvas中CanvasGradient对象用法

    html5 中canvas提供了强大的渲染样式,可以实现一些比较复杂的样式设置,今天学习了CanvasGradient对象可以实现一个颜色的渐变 CanvasGradient对象可以实现两种不同形式的 ...

  8. hdu_4944_FSF’s game

    FSF has programmed a game. In this game, players need to divide a rectangle into several same square ...

  9. python基础——文件访问模式

    文件访问模式 访问模式 说明 r 以只读方式打开文件.文件的指针将会放在文件的开头.这是默认模式. w 打开一个文件只用于写入.如果该文件已存在则将其覆盖.如果该文件不存在,创建新文件. a 打开一个 ...

  10. 汇编:输出寄存器AX中的内容(子程序)

    ;输出寄存器AX中的内容(子程序) DATAS segment DATAS ends CODES segment START: mov AX,DATAS mov DS,AX ;正式代码开始 mov A ...