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. What is machine learning?

    What is machine learning? One area of technology that is helping improve the services that we use on ...

  2. Tmux:终端复用器

    转自Tmux:终端复用器 Tmux 是一个 C 语言编写的终端,它能够在单一窗口中同时访问和控制多个终端.它是一个类似于GNU Screen 的工具.使用它,用户可以在 Linux 系统上管理多个任务 ...

  3. java自定义接口

    compareTo:根据该数值是小于.等于.或大于 val 返回 -1.0 或 1:

  4. JavaScript String支持的辅助format函数+【分页1】

    /** ) {         && ; i < arguments.length; i++) {                 : int.Parse(Request.Par ...

  5. URPF技术白皮书

    URPF技术白皮书 摘    要:本文介绍了URPF的应用背景,URPF主要用于防止基于源地址欺骗的网络攻击行为,例如基于源地址欺骗的DoS攻击和DDoS攻击:随后介绍了URPF的技术原理以及URPF ...

  6. C 语言字符串(译)

    C 语言的 switch 语句非常强大.然而,它不能用字符串作为判断条件,只能用常整数.这是可以理解的,因为 C 的字符串仅仅是数组,它们并不是并不是一个整体. 在某些情况下,将 string 作为 ...

  7. BZOJ_1002_[FJOI2007]_轮状病毒_(递推+高精)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1002 )*&*(^&*^&*^**()*) 1002: [FJOI20 ...

  8. android开发板

    element14-beaglebone-black http://www.embest-tech.cn/shop/star/element14-beaglebone-black-rev-c.html ...

  9. 数学类杂志SCI2013-2014影响因子

    ISSN Abbreviated Journal Title Full Title Category Subcategory Country total Cites IF        2013-20 ...

  10. python导入上级目录中的模块

    python导入同级别模块很方便: import xxx 要导入下级目录页挺方便,需要在下级目录中写一个__init__.py文件 from dirname import xxx 要导入上级目录,可以 ...