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. JPA(4)表表关联关系

    在我们做数据库设计的时候,最烦的就是各种表之间的关联关系了,关联关系有:一对多,多对一,一对一,其中还有单向和双向的区别. 1.双向一对多及多对一映射:既然是双向,那么就是同一类的了:双向一对多关系中 ...

  2. Bootstrap 框架 栅格布局系统设计原理

    如果你是初次接触Bootstrap,你一定会为它的栅格布局感到敬佩.事实上,这个布局系统提供了一套响应式的布局解决方案. 既然这么好用,那他是如何用CSS来实现的呢? 我特意去Bootstrap官方下 ...

  3. HDU 2159---FATE---带限制的完全背包

    HDU   2159 Description 最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务.久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一 ...

  4. map与vector---Email Aliases

    Description Polycarp has quite recently learned about email aliases. Of course, he used to suspect t ...

  5. C#6.0语法糖剖析(一)

    1.自动属性默认初始化 使用代码 "; 编译器生成的代码: public class Customer { [CompilerGenerated] private string kBacki ...

  6. [翻译]:SQL死锁-为什么会出现死锁

    下面这篇对理解死锁非常重要,首先死锁是如何产生的我们要清楚. We already know why blocking occurs in the system and howto detect an ...

  7. CSS3属性(二)

    <html> <head> <title>css2</title> <style type="text/css"> di ...

  8. jQuery 的 ajax

    jQuery load() 方法 jQuery load() 方法是简单但强大的 AJAX 方法. load() 方法从服务器加载数据,并把返回的数据放入被选元素中. $(selector).load ...

  9. android基础开发之RecycleView(1)---基本使用方式

    RecycleView是google为了优化listview,gridview 提供的一个新的控件. 1.android 导入recycleview 在app的gradle里面加入: dependen ...

  10. Git+GitHub 使用小结

    1.Git安装完成后需要做的配置            $ git config --global user.name "Your Name"        $ git confi ...