题目

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

代码

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode dummy(-);
ListNode *p = &dummy,*p1 = l1,*p2 = l2;
int carry = ;
while(p1!=NULL || p2!=NULL){
const int v1 = p1==NULL?:p1->val, v2 = p2==NULL?:p2->val, v = (v1+v2+carry)%;
carry = (v1+v2+carry)/;
p->next = new ListNode(v);
p = p->next;
p1 = p1==NULL ? NULL : p1->next;
p2 = p2==NULL ? NULL : p2->next;
}
if ( carry > ) p->next = new ListNode(carry);
return dummy.next;
}
};

Tips

核心在于判断while停止条件:直到l1和l2都走完了才退出;如果l1或者l2先走完了,就当该位是0。

上面这种思路的好处是可以简化代码。

=========================================

第二次过这道题,已经对思路比较熟悉了;指针定义操作什么的,稍微想了一下;代码还是一次AC。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int carry = ;
ListNode* pre = new ListNode();
ListNode* head = pre;
while ( l1 || l2 )
{
int digit1 = l1 ? l1->val : ;
int digit2 = l2 ? l2->val : ;
int curr_val = (digit1 + digit2 + carry)%;
carry = (digit1 + digit2 + carry)/;
if ( l1 ) l1 = l1->next;
if ( l2 ) l2 = l2->next;
pre->next = new ListNode(curr_val);
pre = pre->next;
}
if ( carry> ) pre->next = new ListNode(carry);
return head->next;
}
};

【Add Two Numbers】的更多相关文章

  1. leetcode 【 Add Two Numbers 】 python 实现

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

  2. CF401D 【Roman and Numbers】

    题意将n(n<=10^18)的各位数字重新排列(不允许有前导零)  求  可以构造几个mod m等于0的数字解法状压f[S][k] 表示选用的位数集合为S,mod m 为k的方案数注意不能有前导 ...

  3. 【Mac系统 + Git】之上传项目代码到github上以及删除某个文件夹

    之前做开发的时候,用过一段时间git代码管理工具,用命令行操作感觉十分高大上,今天我想从头总结一篇Mac系统下如何利用git上传代码到github上的学习. 目录 一.安装Git 二.创建.ssh文件 ...

  4. 【LeetCode445】 Add Two Numbers II★★

    题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...

  5. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

  6. 【LeetCode】2、Add Two Numbers

    题目等级:Medium 题目描述:   You are given two non-empty linked lists representing two non-negative integers. ...

  7. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  8. 【LeetCode-面试算法经典-Java实现】【002-Add Two Numbers (单链表表示的两个数相加)】

    [002-Add Two Numbers (单链表表示的两个数相加)] 原题 You are given two linked lists representing two non-negative ...

  9. 【总文档】rac增加新节点的方法步骤 How to Add Node/Instance or Remove Node/Instance in 10gR2, 11gR1, 11gR2 and 12c Oracle Clusterware and RAC

    [总文档]How to Add Node/Instance or Remove Node/Instance in 10gR2, 11gR1, 11gR2 and 12c Oracle Clusterw ...

随机推荐

  1. html常用的小技能

    在html中有很多常用小技能,记下来,下次直接看自己的,就不用四处找啦! 1.<li>标签去掉点号:list-style-type:none; 去掉前: 去掉后: 2.<li> ...

  2. 远程登录事件ID

    4672.4624 删除本机记录 HKEY_CURRENT_USER \ Software\Microsoft  \ Terminal ServerClientDefault: 删除“此电脑\文档”下 ...

  3. 扫描局域网ip的shell

    # vim /mysh/ipscan.sh #!/bin/bash # scan the local alive ipaddress # -- if [ -f $filename ];then ech ...

  4. 使用Scanner类获取键盘输入的会员卡号,并将该数据存储在变量中,输出这个变量的信息

    package come.one01;import java.util.Scanner; // 导入Scanner类public class One03 { public static void ma ...

  5. 【Python全栈-CSS】background背景

    background背景 一.背景图片 background-image: url("img/num.png"); background-position-x: -200px ; ...

  6. python生成随机数

    import random rnd=rand.uniform(0,10)

  7. Spring Boot 2.x零基础入门到高级实战教程

    一.零基础快速入门SpringBoot2.0 1.SpringBoot2.x课程全套介绍和高手系列知识点 简介:介绍SpringBoot2.x课程大纲章节 java基础,jdk环境,maven基础 2 ...

  8. JDBC中 mysql数据库的连接工具类 Java登录 及增删改查 整理 附带:Navicat Premium 11.0.12中文破解版.zip(下载)mysql数据库工具

    先写一个工具类,有实现MySQL数据库连接的方法,和关闭数据库连接.关闭ResultSet  结果集.关闭PreparedStatement 的方法.代码如下: package com.swift; ...

  9. 第十六篇、OC_按比例适配

    // 屏幕高度 #define XMGHeight [UIScreen mainScreen].bounds.size.height // 屏幕宽度 #define XMGWidth [UIScree ...

  10. 1074: [SCOI2007]折纸origami

    Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 372  Solved: 229[Submit][Status][Discuss] Descriptio ...