题目:

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

解题思路:

这题相当于两个大数相加,只不过这里采用的链表的形式,而不是字符串。

解题时最需注意的是,最后一个节点要考虑会不会进位,over =1时,需要增加一个节点。

实现代码:

#include <iostream>
using namespace std; /**
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 */ 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 && l2 == NULL)
return NULL;
ListNode *l3 = new ListNode(-1);
ListNode *tnode = l3;
int over = 0;
while(l1 && l2)
{
int sum = l1->val + l2->val + over;
ListNode *node = new ListNode(sum % 10);
over = sum / 10;
tnode->next = node;
tnode = tnode->next;
l1 = l1->next;
l2 = l2->next;
}
if(l1 == NULL && l2 == NULL && over)//后一个节点,要考虑有没进位
{
ListNode *node = new ListNode(over);
tnode->next = node;
return l3->next;
} ListNode *left = l1;
if(l2)
left = l2;
while(left)
{
int sum = left->val + over;
ListNode *node = new ListNode(sum % 10);
over = sum / 10;
tnode->next = node;
tnode = tnode->next;
left = left->next; }
if(over)//同样,最后一个节点,要考虑有没进位
{
ListNode *node = new ListNode(over);
tnode->next = node;
}
return l3->next; } };
int main(void)
{
return 0;
}

LeetCode4:Add Two Numbers的更多相关文章

  1. LeetCode-2: Add Two Numbers

    [Problem:2-Add Two Numbers] You are given two non-empty linked lists representing two non-negative i ...

  2. Q2:Add Two Numbers

    2. Add Two Numbers 官方的链接:2. Add Two Numbers Description : You are given two non-empty linked lists r ...

  3. No.002:Add Two Numbers

    问题: You are given two linked lists representing two non-negative numbers.  The digits are stored in ...

  4. leetcode:Add Two Numbers

    题目描述:You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  5. LeetCode之“链表”:Add Two Numbers

    题目链接 题目要求: You are given two linked lists representing two non-negative numbers. The digits are stor ...

  6. LeetCode第[2]题(Java):Add Two Numbers (链表相加)——Medium

    题目难度:Medium 题目: You are given two non-empty linked lists representing two non-negative integers. The ...

  7. LeetCode OJ:Add Two Numbers (相加链表之数)

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  8. 面试题:Add Two Numbers(模拟单链表)

    题干: You are given two non-empty linked lists representing two non-negative integers. The digits are ...

  9. LeetCode第二题:Add Two Numbers

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

随机推荐

  1. Redis教程(十四):内存优化介绍

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/142.html 一.特殊编码: 自从Redis 2.2之后,很多数据类型都 ...

  2. EF架构~通过EF6的DbCommand拦截器来实现数据库读写分离

    回到目录 前几天看了一个基于sqlserver的负载均衡与读写分离的软件Moebius,实现的方式还是不错的,这使得用sqlserver数据库的同学时有机会对数据库进行更有效的优化了

  3. gulp:更简单的自动化构建工具

    目前最流行的两种使用JavaScript开发的构建工具是Grunt和Gulp.为什么使用gulp?因为Gulp更简单.Grunt任务拥有大量的配置,会引用大量你实际上并不需要的对象属性,但是Gulp里 ...

  4. Atitit html5 Canvas 如何自适应屏幕大小

    Atitit  html5 Canvas 如何自适应屏幕大小     可以用JS监控屏幕大小,然后调整Canvas的大小.在代码中加入JS 1 2 3 4 5 6 7 $(window).resize ...

  5. iOS-网络爬虫

    1.iOS开发——网络实用技术OC篇&网络爬虫-使用青花瓷抓取网络数据 2.iOS开发——网络使用技术OC篇&网络爬虫-使用正则表达式抓取网络数据 3.iOS—网络实用技术OC篇&am ...

  6. iOS----------使用 Xcode6或Xcode7配置.pch文件

    刚上手 Xcode6/Xcode7 的人,总会发现之前在 6 之前常常会在“利用名-Prefix.pch”这个文件中来配置我们全局要用到的头文件,但是 xcode6 没有了,人家说,这类东西有时候也会 ...

  7. JDBC操作数据库,第一:jsp插入mysql数据库,坎坷摸索分享

    JSP连接数据库,坎坷摸索了好久,现在终于做好了,分享一下,希望对更多热爱编程学习的人有所帮助!!!谢谢 第一:首先准备的就是已经安装好Mysql,这里不做多叙述,百度可以做到. 然后在mysql数据 ...

  8. WPF入门教程系列二——Application介绍

    一.Application介绍 WPF和WinForm 很相似, WPF与WinForm一样有一个 Application对象来进行一些全局的行为和操作,并且每个 Domain (应用程序域)中仅且只 ...

  9. jQuery贪吃蛇--jQuery学习

    我用JQuery有一段时间了,越来越体会到其强大之处,于是自己尝试写了一个贪吃蛇小游戏,拿来与网友分享一下. 1. 了解JQuery.Timers 除用到了jQuery1.5.1之外,我还用到了jQu ...

  10. 浅谈Winform事件的实现以及模拟其事件的实现(附实现源码)

    当我们初学Winform的时候被其神奇的事件功能所吸引,当点击一个按钮时,便会跳到我们所写的点击方法当中去.然而这并不符合我们对方法的理解,究竟.net在后面帮助我们实现了什么.我们怎样模拟其事件的实 ...