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. csharp:asp.net Importing or Exporting Data from Worksheets using aspose cell

    using System; using System.Data; using System.Configuration; using System.Collections; using System. ...

  2. MAC 隐藏文件的显示

    显示 defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏 defaults write com.apple.finder A ...

  3. Android sdk manager不能更新下载缓慢的解决方法

    通常情况下,下载Android SDK需要连接谷歌的服务器进行下载,由于国内水深火热的网络,速度基本为0.好在国内也有一个更新的镜像地址.本文章介绍如何在不FQ的情况下,使用国内镜像地址,更新andr ...

  4. 【GOF23设计模式】装饰模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_装饰模式.IO流底层架构.装饰和桥接模式的区别 package com.test.decorator; /** * Com ...

  5. SAPScript、Smartforms动态打印图像或者背景图片

    在利用 SMARTFORMS 进行打印的时候有时候要求输出的图片可能是随着打印内容的不同而不同了,也就是动态输出图片,SMARTFORMS的提供了相关的支持技术,下面是实现截图 1.创建要显示的图片 ...

  6. Java中的继承与组合(转载)

    本文主要说明Java中继承与组合的概念,以及它们之间的联系与区别.首先文章会给出一小段代码示例,用于展示到底什么是继承.然后演示如何通过“组合”来改进这种继承的设计机制.最后总结这两者的应用场景,即到 ...

  7. How to Use Telnet to Test SMTP Communication

    Topic Last Modified: 2005-05-24 Telnet is an extremely useful tool for troubleshooting issues relate ...

  8. 换iphone5屏幕你花了多少钱?不防我们看下市场的批发价格

    看来人家批发价也不便宜啊,你修一块花了多少米 免费b2b平台  US $1 - 79.99 / Piece Factory Price for iphone 5" lcd alibaba   ...

  9. 我在用的mac软件(2)-终端环境之zsh和z(*nix都适用)

    继续上篇介绍我的终端环境.这篇介绍zsh和z,其实这不局限于os x,在所有的*nix系统中都是可用的. zsh zsh作为bash的替代品,自然很多人要问:why zsh? 在Zsh Worksho ...

  10. 去掉 Android工程中让人很不爽的“黄色警告”

    一:问题       二:解决方法 (1)选择android工程,右键Android Tools —> Clear Lint Markers 这种方式能够清除android工程里面的所有警告信息 ...