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 ;
}

最新文章

  1. 在windows上安装scikit-learn开发环境
  2. Aspose.Cells 设置背景颜色
  3. 【区间dp】codevs1966 乘法游戏
  4. hdu 5753 Permutation Bo
  5. Lintcode: Rehashing
  6. 如何使用XAMPP本地搭建一个属于你自己的网站
  7. 生成24位字符串ID__IdGenerator.java
  8. httpcomponents 学习1--并发多线程GET
  9. oracle 绿色版本 instantclient 使用说明
  10. PHP 判断协议是否为HTTPS
  11. NAND Flash内部结构简介
  12. Android(java)学习笔记223:上下文的区分
  13. OpenCV学习(1)OpenCV简介
  14. [android]Gradle: 执行失败的任务 &#39;: processDebugManifest&#39;
  15. Ubuntu访问window下的磁盘分区出现“Error mounting /dev/sda5 at/media”错误的解决方法
  16. js 操作本地sqlite
  17. C# — Windows服务安装后自动停止问题
  18. iOS -- Effective Objective-C 阅读笔记 (5)
  19. Eciplce ALT+/失效的解决方法
  20. A. Srdce and Triangle 几何题

热门文章

  1. ubuntu16.04安装中文输入法(转)
  2. 如何将UI5应用部署到Fiori On-Premise和On-Cloud的Launchpad上去
  3. flex布局-常用布局
  4. linux命令之awk命令
  5. MySQL 数据库和一些常用命令的使用
  6. python3中使用HTMLTestRunner.py报ImportError: No module named &#39;StringIO&#39;的解决办法
  7. caffe的损失函数
  8. window7配置python3.3 + django + apache24 + mod_wsgi
  9. LeetCode106. Construct Binary Tree from Inorder and Postorder Traversal
  10. 基于 win7下虚拟机的 GNSS-SDR安装过程