2.5 给定两个用链表表示的整数,每个结点包含一个数位。这些数位是反向存放的,也就是个位排在链表首部。编写函数对这两个整数求和,并用链表形式返回结果。

示例:

输入: (7->1->6)+(5->9->2),即617+295.

输出:2->1->9,即912.

进阶:

假设这些数位是正向存放的

示例:

输入:(6->1->7)+(2->9->5),即617+295.

输出:9->1->2,即912.

逆向存放时,C++实现:

#include<iostream>
#include<new>
using namespace std; struct ListNode
{
int val;
ListNode *next;
ListNode(int x):val(x),next(NULL) {}
}; void createList(ListNode *&L,int arr[],int n)
{
int i;
ListNode *p=NULL;
for(i=; i<n; i++)
{
ListNode *tmp=new ListNode(arr[i]);
if(L==NULL)
{
L=tmp;
p=tmp;
}
else
{
p->next=tmp;
p=tmp;
}
}
} ListNode *merge(ListNode *L1,ListNode *L2)
{
if(L1==NULL&&L2==NULL)
return NULL;
if(L1==NULL||L2==NULL)
return L1!=NULL?L1:L2;
ListNode *p=L1;
ListNode *pre=L1;
ListNode *q=L2;
int carry=;
while(p&&q)
{
int sum=p->val+q->val+carry;
p->val=sum%;
carry=sum/;
pre=p;
p=p->next;
q=q->next;
}
while(p)
{
int sum=p->val+carry;
cout<<sum<<endl;
p->val=sum%;
carry=sum/;
pre=p;
p=p->next;
}
ListNode *tmp=NULL;
while(q)
{
int sum=q->val+carry;
tmp=new ListNode(sum%);
carry=sum/;
pre->next=tmp;
pre=tmp;
q=q->next;
}
if(carry==)
{
tmp=new ListNode(carry);
pre->next=tmp;
}
return L1;
} int main()
{
ListNode *L1=NULL;
ListNode *L2=NULL;
int arr1[]={,,};
int arr2[]={,};
createList(L1,arr1,);
createList(L2,arr2,);
ListNode *head=merge(L1,L2);
ListNode *p=head;
while(p)
{
cout<<p->val<<" ";
p=p->next;
}
cout<<endl;
}

正向存放时,可以先利用头插入将两个链表逆转,然后按照上面的过程求和。

careercup-链表 2.5的更多相关文章

  1. [CareerCup] 2.1 Remove Duplicates from Unsorted List 移除无序链表中的重复项

    2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this p ...

  2. [CareerCup] 2.2 Kth to Last Element of Linked List 链表的倒数第k个元素

    2.2 Implement an algorithm to find the kth to last element of a singly linked list. 这道题让我们求链表中倒数第k个元 ...

  3. [CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点

    2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access ...

  4. [CareerCup] 2.4 Partition List 划分链表

    2.4 Write code to partition a linked list around a value x, such that all nodes less than x come bef ...

  5. [CareerCup] 2.6 Linked List Cycle 单链表中的环

    2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...

  6. [CareerCup] 2.7 Palindrome Linked List 回文链表

    2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...

  7. [CareerCup] 4.4 Create List at Each Depth of Binary Tree 二叉树的各层创建链表

    4.4 Given a binary tree, design an algorithm which creates a linked list of all the nodes at each de ...

  8. [CareerCup] 17.13 BiNode 双向节点

    17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...

  9. 二叉树系列 - 二叉搜索树 - 线性时间内把有序链表转化为BST

    引言 本文来自于Google的一道题目: how to merge two binary search tree into balanced binary search tree. how to me ...

  10. Careercup - Google面试题 - 5735304249999360

    2014-05-03 23:18 题目链接 原题: Insert a element in a sorted circular linked list 题目:题意简单明了,向一个有序的循环单向链表中插 ...

随机推荐

  1. 批量建立EXCHANGE邮件帐号建立三部曲

    第一步:从AD里导出用户名(可以基于OU),将输出的CSV的DN列删除,并去除可能的测试及其它用途用户名. csvde -f users-gz.csv -d "ou=MKT gz,dc=xm ...

  2. 【HDU 1828】 Picture (矩阵周长并,线段树,扫描法)

    [题目] Picture Problem Description A number of rectangular posters, photographs and other pictures of ...

  3. 【BZOJ 2005】[Noi2010]能量采集 (容斥原理| 欧拉筛+ 分块)

    能量采集 Description 栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量.在这些植物采集能量后,栋栋再使用一个能量汇集机器把这些植物采集到的能量汇集到一起. 栋栋 ...

  4. 使用Common.Logging+log4net规范日志管理

    Common.Logging+(log4net/NLog/) common logging是一个通用日志接口,log4net是一个强大的具体实现,也可以用其它不同的实现,如EntLib的日志.NLog ...

  5. NOI2005瑰丽华尔兹

    1499: [NOI2005]瑰丽华尔兹 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 893  Solved: 508[Submit][Status] ...

  6. 备份及还原Xcode的模拟器

    http://blog.csdn.net/it_magician/article/details/8749876 每次更新或者重装Xcode之后,最麻烦的莫过于各个模拟器的安装了,因为下载速度实在让人 ...

  7. android 获取设备唯一标识完美解决方案

    /** * deviceID的组成为:渠道标志+识别符来源标志+hash后的终端识别符 * * 渠道标志为: * 1,andriod(a) * * 识别符来源标志: * 1, wifi mac地址(w ...

  8. (转载)C++:STL标准入门汇总

    (转载)http://www.cnblogs.com/shiyangxt/archive/2008/09/11/1289493.html 学无止境!!! 第一部分:(参考百度百科) 一.STL简介 S ...

  9. java基础(二十)IO流(三)

    这里有我之前上课总结的一些知识点以及代码大部分是老师讲的笔记 个人认为是非常好的,,也是比较经典的内容,真诚的希望这些对于那些想学习的人有所帮助! 由于代码是分模块的上传非常的不便.也比较多,讲的也是 ...

  10. leecode Binary Tree Level Order Traversal II java

    做完这道题,只能说基本功很重要,数组中套数组就不会用了,过几天吧1做了,看自己到底等没. https://oj.leetcode.com/problems/binary-tree-level-orde ...