careercup-链表 2.5
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的更多相关文章
- [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 ...
- [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个元 ...
- [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 ...
- [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 ...
- [CareerCup] 2.6 Linked List Cycle 单链表中的环
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
- [CareerCup] 2.7 Palindrome Linked List 回文链表
2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...
- [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 ...
- [CareerCup] 17.13 BiNode 双向节点
17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...
- 二叉树系列 - 二叉搜索树 - 线性时间内把有序链表转化为BST
引言 本文来自于Google的一道题目: how to merge two binary search tree into balanced binary search tree. how to me ...
- Careercup - Google面试题 - 5735304249999360
2014-05-03 23:18 题目链接 原题: Insert a element in a sorted circular linked list 题目:题意简单明了,向一个有序的循环单向链表中插 ...
随机推荐
- C语言可变参数在宏定义中的应用
在C语言的标准库中,printf.scanf.sscanf.sprintf.sscanf这些标准库的输入输出函数,参数都是可变的.在调试程序时,我们可能希望定义一个参数可变的输出函数来记录日志,那么用 ...
- [JavaScript] js实现简单的代码运行框
<script type="text/javascript">// <![CDATA[ function runCode(obj) { var winname = ...
- 限制sqlserver最大内存后无法连接-EXEC sp_configure max server memory
在sql server 中设置了过小的 "max server memory"最大内存后,sqlserver可启动,但是无法连接. 网络上流行的"sqlserver 内存 ...
- GSM、GPRS、EDGE、2G、3G与WAP的关系
1.GSM(Global System of Mobile communication)即全球移动通讯系统: 是目前使用人数最大的移动通信网络,就是2G的移动通信技术,是一种电路交换系统.这种网络仅提 ...
- Setup SQL Server 2008 Maintenance Plan Email Notifications
一条龙作完,如何设置EXCHANGE的操作员邮件通知.. ~~~~ http://808techblog.com/2009/07/setup-sql-server-2008-maintena.html ...
- java api如何获取kafka所有Topic列表,并放置为一个list
kafka内部所有的实现都是通过TopicCommand的main方法,通过java代码调用API,TopicCommand.main(options)的方式只能打印到控制台,不能转换到一个list. ...
- 【POJ 1988】 Cube Stacking (带权并查集)
Cube Stacking Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)id ...
- [转贴]JAVA :CXF 简介
Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache CXF 了,以下简称为 CXF.CXF 继 ...
- C++类的大小
C++类的大小 一个空类class A{};的大小为什么是1,因为如果不是1,当定义这个类的对象数组时候A objects[5]; objects[0]和objects[1]就在同一个地址处,就无 ...
- linux命令中,执行一个程序,后面加上&, 代表的意思是什么?
后台执行.也就是执行这个程序的同时,你的终端同时还能够做其他的事情,如果不加这个符号,那么你执行这个程序后,你的终端只能等这个程序执行完成才能够继续执行其他的操作 . 如:启动etcd: ./etcd ...