LeetCode 2 Add Two Sum 解题报告

LeetCode第二题 Add Two Sum 首先我们看题目要求:

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

题目分析

这道题目是一道基础的链表题,给定两个非负数,它们是按照逆序存储的,每个节点值保留一个数值,要求输出这两个数之和,返回结果链表。本道题目主要是考察链表遍历的一些操作。

思路
1.首先用两个指针,分别同时遍历两个链表,按位相加,设置相应进位标志。
2.当两个链表长度不一致时,结束按位相加的遍历之后,将剩余链接接上
3.需要注意连续进位。

以下给出完整的测试代码,在这里为了操作方便,在遍历时统一把数据放到了list的容器中,主要是担心,对于链接,连续进位时直接用指针new出错,直接将每一位Push到list中,最后直接通过list构造出一个ListNode的链表

#include<iostream>
#include<list>
using namespace std; struct ListNode
{
int val;
ListNode * next;
ListNode(int x):val(x),next(NULL){}
}; ListNode * createListNode( int * arr, int num)
{
int i = 0;
ListNode * head = new ListNode(arr[0]);//head pointer
ListNode * p1 = head;
ListNode * p2 = head;
if(num == 1)
{
head->next = NULL;
return head;
}
else
{
for(i = 1; i < num; i++)
{
p1 = new ListNode(arr[i]);
p2->next = p1;
p2 = p1;
}
p1->next = NULL;
}
return head;
} class Solution
{
public:
ListNode * createListNode2( list<int> iList)//
{
int num = iList.size();
list<int>::iterator it = iList.begin();
ListNode * head = new ListNode(*it);//head pointer
ListNode * p1 = head;
ListNode * p2 = head;
it++;
if(num == 1)
{
head->next = NULL;
return head;
}
else
{
for(; it != iList.end(); it++)
{
p1 = new ListNode(*it);
p2->next = p1;
p2 = p1;
}
p1->next = NULL;
}
return head;
} ListNode * addTwoNumbers (ListNode * ln1,ListNode * ln2)
{ list<int> result;
ListNode * p;
ListNode * p1 = ln1;
ListNode * p2 = ln2;
int carryFlag = 0;
int curNum = 0;
while(p1 != NULL && p2 != NULL)
{
curNum = (p1->val + p2->val + carryFlag)%10;
if((p1->val + p2->val + carryFlag) >= 10)
carryFlag = 1;
else
carryFlag = 0;
result.push_back(curNum);
p1 = p1->next;
p2 = p2->next;
}
if(p1 == NULL && p2 == NULL)
{
if (carryFlag == 1)
result.push_back(carryFlag);
}
else if(p1 != NULL && p2 == NULL )
{
while(p1 != NULL)
{
curNum = (p1->val+carryFlag) %10;
if(p1->val + carryFlag >= 10)
carryFlag = 1;
else
carryFlag = 0;
result.push_back(curNum);
p1 = p1->next;
}
if(carryFlag ==1 )
result.push_back(carryFlag);
}
else if(p1 == NULL && p2 != NULL)
{
while(p2 != NULL)
{
curNum = (p2->val+carryFlag) %10;
if(p2->val + carryFlag >= 10)
carryFlag = 1;
else
carryFlag = 0; result.push_back(curNum);
p2 = p2->next;
}
if(carryFlag == 1 )
result.push_back(carryFlag);
} list<int>::iterator it = result.begin();
for(;it != result.end(); it++)
cout<<*it;
return createListNode2(result);
}
}; int main ()
{
int arr1[] = {1};
int arr2[] = {9,9};
Solution s1;
ListNode *l1 = createListNode(arr1,1);
ListNode *l2 = createListNode(arr2,2);
s1.addTwoNumbers(l1,l2);
return 0;
return 0;
}

LeetCode 2 Add Two Sum 解题报告的更多相关文章

  1. 【LeetCode】494. Target Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  2. 【LeetCode】39. Combination Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:[htt ...

  3. LeetCode 2. Add Two Numbers 解题报告

    题意: 有两个链表,它们表示逆序的两个非负数.例 (2 -> 4 -> 3)表示342,求两个数字的和,并用同样的方式逆序输出.如342+465 = 807,你需要把结果表达为(7 -&g ...

  4. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  5. 【LeetCode】385. Mini Parser 解题报告(Python)

    [LeetCode]385. Mini Parser 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/mini-parser/ ...

  6. 【LeetCode】809. Expressive Words 解题报告(Python)

    [LeetCode]809. Expressive Words 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  7. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  8. 【LeetCode】848. Shifting Letters 解题报告(Python)

    [LeetCode]848. Shifting Letters 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  9. 【LeetCode】554. Brick Wall 解题报告(Python)

    [LeetCode]554. Brick Wall 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

随机推荐

  1. mariadb connector bug

    为了解决http://www.cnblogs.com/zhjh256/p/5807086.html的问题测试mariadb connector,常规的增删改查没有问题. 这货本来是为了解决存储过程bu ...

  2. mysql awr v1.0.1发布

    现发布mysql awr v1.0.1 修复问题: 1.galera cluster下flush table/index_statistcs时如果系统中业务ddl频繁可能会导致很多进程处于prepar ...

  3. elasticsearch的mapping映射

    Mapping简述 Elasticsearch是一个schema-less的系统,但并不代表no shema,而是会尽量根据JSON源数据的基础类型猜测你想要的字段类型映射.Elasticsearch ...

  4. For循环语句的使用

    一.For循环语句 说明:For循环用于循环次数已经确定的情况下.  格式:for(循环变量赋初值; 循环条件; 循环变量增值)       {             ·····语句  } 举例:求 ...

  5. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q88-Q90)

    Question 88You have a Microsoft .NET Framework console application that uses the SharePoint client o ...

  6. java多线程系列2-线程控制

    前面的文章已经介绍了有关线程的调度,接下来介绍如何使用方法对线程进行控制 1.线程休眠 public static void sleep(long millis) /* * 线程休眠 * public ...

  7. UISegmentedControl的使用

    #import "SegmentedControlTestViewController.h" @implementation SegmentedControlTestViewCon ...

  8. WPF + Caliburn.Micro +ActionMessage事件绑定

    ActionMessage事件绑定是个人觉的算是CM的精髓了,比如说我在View里面放个button,我们要在他的click事件里面写东西,怎么写.如果是WPF我们直接在CS里面写就可以.但是CM不行 ...

  9. 整理了一些常用的jQuery动画事件

    部分jQuery常用的动画函数,整理了一下,在做交互式页面的时候挺有用的 .css('a','12px');.css({  a:'12px',  b:'#fff'}); .show();.hide() ...

  10. celery 快速入门教程 celery 定时器

    当然首先得安装celery和rabbitmq-server,如果有redis需要安装redis 安装Redis $ yum install redis 启动 Redis $redis-server 检 ...