题目:

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. make file教程(转)

    最近在学习Linux下的C编程,买了一本叫<Linux环境下的C编程指南>读到makefile就越看越迷糊,可能是我的理解能不行. 于是google到了以下这篇文章.通俗易懂.然后把它贴出 ...

  2. Nodejs·进程

    之前对这部分的内容很感兴趣,没想到读起来有点晦涩,还是因为对服务器的知识不是很了解. 说道服务器一般人都会想到tomcat或者Jboss或者weblogic,现在流行起来的Node总让人不太放心,JS ...

  3. Android开发学习之路-抢红包助手开发全攻略

    背景:新年之际,微信微博支付宝红包是到处飞,但是,自己的手速总是比别人慢一点最后导致红包没抢到,红包助手就应运而生. 需求:收到红包的时候进行提醒,然后跳转到红包的界面方便用户 思路:获取“读取通知信 ...

  4. Atitit 编程语言常用算法attilax总结

    Atitit 编程语言常用算法attilax总结 1. 编译算法分类and   数据操作算法.1 1.1. Tab driver stat  状态转换表格算法1 1.2. Nest case 词法分析 ...

  5. Atitit 颜色平均值cloor grb hsv模式的区别对比

    Atitit 颜色平均值cloor grb hsv模式的区别对比 使用hsv模式平均后会变得更加的靓丽一些..2 public class imgT { public static void main ...

  6. Atitit 图像处理—图像形态学(膨胀与腐蚀)

    Atitit 图像处理-图像形态学(膨胀与腐蚀) 1.1. 膨胀与腐蚀1 1.2. 图像处理之二值膨胀及应用2 1.3. 测试原理,可以给一个5*5pic,测试膨胀算法5 1.4. Photoshop ...

  7. salesforce 零基础学习(二十)简单APP制作

    本篇参考链接:https://developer.salesforce.com/trailhead/project/salesforce_developer_workshop 本篇讲述的是最简单的AP ...

  8. salesforce 零基础学习(十七)Trigger用法

    看本篇之前可以相应阅读以下Trigger相关文章: 1.https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigge ...

  9. Liferay7 BPM门户开发之41: Expando API入门

    Expando 是liferay的一种自定义表格扩展的方式,从5.0就已存在 , 可以在运行时新建表格\字段\行\值. 这是一种Service Builder之外的轻量级替代扩展方式,不必像Servi ...

  10. WP、Win10开发或者WPF开发时绘制自定义窗体~例如:一个手机

    WP and Win10 效果:(数字是参考值,和UI无关) <Page x:Class="_05.AllControls._BorderUsePage" xmlns=&qu ...