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 题目:题意简单明了,向一个有序的循环单向链表中插 ...
随机推荐
- Bootstrap 轮播(Carousel)插件
Bootstrap 轮播(Carousel)插件是一种灵活的响应式的向站点添加滑块的方式.除此之外,内容也是足够灵活的,可以是图像.内嵌框架.视频或者其他您想要放置的任何类型的内容. 如果您想要单独引 ...
- 【HDU 5370】 Tree Maker(卡特兰数+dp)
Tree Maker Problem Description Tree Lover loves trees crazily. One day he invents an interesting gam ...
- java WeakReference
在Java 1.2中就引入了java.lang.ref这个包,WeakReference就属于这个包.WeakReference是干嘛的呢,一言弊之,它是和Java中的垃圾回收相关的.如果一个对象只有 ...
- 【HDOJ】3496 Watch The Movie
二维费用背包. #include <stdio.h> #include <string.h> #define mymax(a, b) (a>b) ? a:b ][]; ] ...
- zabbix监控zookeeper
在github找到一个不错的模板,直接导入使用.下载地址: https://github.com/zhujinhe/zookeeper-zabbix-template 监控项监控类型为外部检查 zab ...
- 【Android 复习】 : Activity之间传递数据的几种方式
在Android开发中,我们通常需要在不同的Activity之间传递数据,下面我们就来总结一下在Activity之间数据传递的几种方式. 1. 使用Intent来传递数据 Intent表示意图,很多时 ...
- 解决Subclipse1.6在64位JDK下不可用的问题
Failed to load JavaHL Library. These are the errors that were encountered: 需要下载SVNKit Adapter Sub ...
- Service Oriented Architecture and WCF 【转】
http://www.codeproject.com/Articles/515253/Service-Oriented-Architecture-and-WCF Introduction This a ...
- Good Numbers
Problem Description If we sum up every digit of a number and the result can be exactly divided by 10 ...
- Android项目开发全程(三)-- 项目的前期搭建、网络请求封装是怎样实现的
在前两篇博文中已经做了铺垫,下面咱们就可以用前面介绍过的内容开始做一个小项目了(项目中会用到Afinal框架,不会用Afinal的童鞋可以先看一下上一篇博文),正所谓麻雀虽小,五脏俱全,这在里我会尽量 ...