题目:

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)

Output: 7 -> 0 -> 8

分析:

这个题目涉及到链表知识,关于指针、链表自从接触就是我的弱势,到现在那么多年过去了,依然没有改变。但是,既然撞上了,还是必须义无反顾的应战的。分析一下题目,它是说有两个链表,每个链表存储非负数值,链表的每个节点都是一位个位数字,数值倒序存储在链表中,两者求和并以链表的形式返回。
题目信息分析完毕后,开始动手吧,哎,依然是头皮发麻呀~

AC代码:

这个题目的代码编写到调试,再到最后的AC,可真是花费了不少功夫呀,好在功夫不负有心人,嘿嘿,问题代码就不上传了,下面贴出AC代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution
{
public:
ListNode *addTwoNumbers(ListNode* l1 , ListNode *l2)
{
if(l1 == NULL)
return l2;
if(l2 == NULL)
return l1; vector<int> v1;
vector<int> v2;
ListNode *head=NULL , *rear=NULL;
while(l1 != NULL)
{
v1.push_back(l1->val);
l1 = l1->next;
}
while(l2 != NULL)
{
v2.push_back(l2->val);
l2 = l2->next;
} if(v1.size() < v2.size())
{
for(int k=v1.size() ; k<v2.size() ; k++)
v1.push_back(0);
}else
{
for(int k=v2.size() ; k<v1.size() ; k++)
v2.push_back(0);
}
int temp = 0;
int value = 0;
for(int j=0 ; j<v1.size() ; j++)
{
int sum = v1[j] + v2[j] + temp;
temp = sum / 10;
value = sum % 10;
ListNode *node = new ListNode(value);
if(head == NULL)
head = node;
if(rear == NULL)
rear = node;
else
{
rear->next = node;
rear = rear->next;
}
}
if(temp != 0 && rear!=NULL)
{
ListNode *node = new ListNode(temp);
rear->next = node;
}
return head;
}
};

测试Main函数:

为了方便测试,下面提供Main测试代码,说明,在LeetCode页面提交代码,只需要上传Solution类即可:
int main()
{
ListNode *l1=NULL , *r1=NULL, *l2 = NULL , *r2=NULL , *result=NULL;
int arr1[3] = {2,4,3};
int arr2[3] = {5,6,4};
for(int i=0 ; i<3 ; i++)
{
ListNode *node1 = new ListNode(arr1[i]);
ListNode *node2 = new ListNode(arr2[i]);
if(l1 == NULL)
l1 = node1;
if(r1 == NULL)
r1 = node1;
else{
r1->next = node1;
r1 = r1->next;
}
if(l2 == NULL)
l2 = node2;
if(r2 == NULL)
r2 = node2;
else{
r2->next = node2;
r2 = r2->next;
}
}
Solution s;
result = s.addTwoNumbers(l1,l2);
for( ; result!=NULL ; result=result->next)
cout<<result->val<<"->";
cout<<endl;
system("pause");
return 0;
}

LeetCode(2)Add Two Numbers的更多相关文章

  1. LeetCode(68)-Compare Version Numbers

    题目: Compare two version numbers version1 and version2. If version1 > version2 return 1, if versio ...

  2. LeetCode(258) Add Digits

    题目 Given a non-negative integer num, repeatedly add all its digits until the result has only one dig ...

  3. LeetCode(165) Compare Version Numbers

    题目 Compare two version numbers version1 and version2. If version1 > version2 return 1, if version ...

  4. LeetCode(67) Add Binary

    题目 Given two binary strings, return their sum (also a binary string). For example, a = "11" ...

  5. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  6. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  7. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  8. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  9. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

随机推荐

  1. mysql写存储过程并定时调用

    设置一个定时任务:运行以下SQL -- 创建一个表test:字段endtime CREATE TABLE test (endtime DATETIME);   -- 创建函数 :向test插入endt ...

  2. Fabric 简单使用

    Fabric 简单使用 最近公司组织压测系统,要在多台机子上部署代码,可是机子上的代码与生产环境不一样,需要修改代码,还有有问题的地方要修改,然后再发代码.这边一共有7台服务务器,重新发代码,要一台一 ...

  3. 死磕 java并发包之AtomicStampedReference源码分析(ABA问题详解)

    问题 (1)什么是ABA? (2)ABA的危害? (3)ABA的解决方法? (4)AtomicStampedReference是什么? (5)AtomicStampedReference是怎么解决AB ...

  4. find搜索文件系统,实时搜索

    find搜索文件系统.实时搜索 find[目录][条件][动作] [目录] 不输入目录代表当前目录 find find /etc ----------------------------------- ...

  5. hihocoder1776 序列

    思路: 考虑从左至右依次向每个位置放置数字,对于第i个位置,以i为结尾的i个前缀和模P是不能相等的(因为不存在和为P的倍数的子串),所以第i个位置只能放置P - i个不同的数字.则答案就是(P - 1 ...

  6. poj 2406 Power Strings 周期问题

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 48139   Accepted: 20040 D ...

  7. 跨平台C++开源代码的两种常用编译方式

    作者:朱金灿 来源:http://blog.csdn.net/clever101 跨平台C++开源代码为适应各种编译器的编译,采用了两种方式方面来适配.一种是makefile方式.以著名的空间数据格式 ...

  8. spark常用参数

    val conf = new SparkConf().setAppName("WordCount_groupBy").setMaster("local") // ...

  9. nmon安装和使用介绍

    使用参考地址:百度中搜索 nmon 博客园 使用文档参考地址:http://nmon.sourceforge.net/pmwiki.php?n=Site.Documentation nmmon地址:h ...

  10. MySQL索引使用等