题目:

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. 缓存篇~第六回 Microsoft.Practices.EnterpriseLibrary.Caching实现基于方法签名的数据集缓存

    返回目录 这一讲中主要是说EnterpriseLibrary企业级架构里的caching组件,它主要实现了项目缓存功能,它支持四种持久化方式,内存,文件,数据库和自定义,对于持久化不是今天讨论的重要, ...

  2. Atitit 图像处理知识点  知识体系 知识图谱

    Atitit 图像处理知识点  知识体系 知识图谱 图像处理知识点 图像处理知识点体系 v2 qb24.xlsx 基本知识图像金字塔op膨胀叠加混合变暗识别与检测分类肤色检测other验证码生成 基本 ...

  3. OutputCache属性详解(一)一Duration、VaryByParam

    目录 OutputCache概念学习 OutputCache属性详解(一) OutputCache属性详解(二) OutputCache属性详解(三) OutputCache属性详解(四)— SqlD ...

  4. PHP 函数基础

    1.简单函数     函数四要素:返回类型,函数名,参数列表,函数体 function Show(){      echo "hello";}Show();         输出了 ...

  5. 阿里云上安装mysql步骤/ 阿里云ECS搭建Java+mysql+tomcat环境

    使用阿里云ECS挺长一段时间了.这两天碰巧朋友小白让我一步一步教他在ECS上搭建Java+mysql+tomcat环境,所以把在这里把步骤在这简单整理了一下,以便需要的人查阅. 我购买的阿里云服务器系 ...

  6. jquery的html,text,val

    .html()用为读取和修改元素的HTML标签 .text()用来读取或修改元素的纯文本内容 .val()用来读取或修改表单元素的value值. 这三个方法功能上的对比 .html(),.text() ...

  7. mysql创建数据库

    mysql创建数据库 创建utf8编码的数据库 CREATE DATABASE IF NOT EXISTS my_db default character set utf8 COLLATE utf8_ ...

  8. 项目中是用eCharts

    1.首先在项目中引入echart.js库. <!DOCTYPE HTML> <%@page contentType="text/html; charset=UTF-8&qu ...

  9. css知多少(5)——选择器

    1. 引言 从本节开始,就进入本系列的第二个部分——css和html的结合——说白了就是选择器. CSS中定义了样式,如何将这些样式设置到相应的html节点上?就不得不通过选择器.让浏览器知道css选 ...

  10. codeforce Pashmak and Buses(dfs枚举)

    /* 题意:n个同学,k个车, 取旅游d天! 要求所有的学生没有两个或者两个以上的在同一辆车上共同带d天! 输出可行的方案! 对于d行n列的矩阵,第i行第j列表示的是第i天第j个同学所在的车号! 也就 ...