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. nutch 很多url unfetched的原因

    bin/hadoop jar apache-nutch-1.7.job org.apache.nutch.crawl.CrawlDbReader crawl/crawldb -stats -sort ...

  2. JVM 学习笔记(一)

    JVM  ----Java  Virtual Machine   (熟称:JAVA虚拟机),JVM 在执行JAVA程序的过程中将内容划分为若干个区域,其有各自的用途和管理机制.如下图: 1.  程序计 ...

  3. DDD领域驱动设计和实践(转载)

    -->目录导航 一. DDD领域驱动设计介绍 1. 什么是领域驱动设计(DDD) 2. 领域驱动设计的特点 3. 如果不使用DDD? 4. 领域驱动设计的分层架构和构成要素 5. 事务脚本和领域 ...

  4. Hicharts弄个样例

    前端的事情,但最好自己要了解一下,能作个最简单的东东出来... 样例,需要的时候,用用,就喟给它一样模板数据即可. PYTHON,把字典的键值和KEY值匹配成列表即可. $(function () { ...

  5. log.isDebugEnabled()的使用

    转自: http://huangxx.iteye.com/blog/190693 在使用log4j,common-log这样的log框架时,发现很多代码中这样写 if   (log.isDebugEn ...

  6. redis key和value数据类型

    exists key del key1 key2 Redis 的vauleredis 提供五种数据类型:string,hash,list,set 及sorted set. string 是最基本的类型 ...

  7. Chapter 17 Replication

    Chapter 17 Replication Table of Contents 17.1 Replication Configuration 17.2 Replication Implementat ...

  8. jdk jre jvm 关系

    很多朋友可能跟我一样,已经使用JAVA开发很久了,可是对JDK,JRE,JVM这三者的联系与区别,一直都是模模糊糊的. 今天特写此文,来整理下三者的关系. JDK : Java Development ...

  9. 应用程序域 z

    应用程序域(AppDomain)已经不是一个新名词了,只要熟悉.net的都知道它的存在,不过我们还是先一起来重新认识下应用程序域吧,究竟它是何方神圣. 应用程序域 众所周知,进程是代码执行和资源分配的 ...

  10. HIbernate java.lang.AbstractMethodError: com.microsoft.jdbc.base.BaseDatabaseMetaData.supportsGetGeneratedKeys()Z

    [HIbernate]java.lang.AbstractMethodError: com.microsoft.jdbc.base.BaseDatabaseMetaData.supportsGetGe ...