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. Linux安装中文字体包

    进入rhel5.5安装盘/Server路径找到字体安装包: fonts-chinese-3.02-12.el5.noarch.rpm fonts-ISO8859-2-75dpi-1.0-17.1.no ...

  2. make知识

    makelist 语法 https://cmake.org/cmake/help/v3.10/manual/cmake-language.7.html CMakeLists.txt I am of t ...

  3. @RequestMapping,@ResponseBody,@RequestBody用法

    本文转载:http://blog.csdn.net/ff906317011/article/details/78552426 1.@RequestMapping 国际惯例先介绍什么是@RequestM ...

  4. Family Gathering at Christmas(思维题)

    Family Gathering at Christmas 时间限制: 1 Sec  内存限制: 128 MB提交: 13  解决: 4[提交] [状态] [讨论版] [命题人:admin] 题目描述 ...

  5. Spring boot 集成Spring Security

    依赖jar <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...

  6. mysql数值函数

    abs(x) -- 绝对值 abs(-10.9) = 10 format(x, d) -- 格式化千分位数值 format(1234567.456, 2) = 1,234,567.46 ceil(x) ...

  7. 旧文备份: CANopen的LSS子协议中文翻译

    有关节点地址和网络波特率的在线设置等:下载

  8. 数据库可视化工具简介以及pymysql的使用

    1.可视化工具Navicat 我们自己开发测试时,可以使用该可视化工具,以图形界面的形式操作数据库 在生产环境中,为了显示自己的逼格,一般不建议使用它 官网下载:https://www.navicat ...

  9. python while循环与for循环

    今天刚看了一下python的while和for循环,所以打算记录一下: while语句是python中的循环条件语句,while 判断条件 : pass break 例如: i = 1 sum = 1 ...

  10. 当Java遇见了Html--Servlet篇

    ###一.什么是servlet servlet是在服务器上运行的小程序.一个servlet就是一个 java类,并且通过"请求-响应"编程模型来访问的这个驻留在服务器内存里的程序. ...