题目描述:

You are given two non-empty linked lists representing two non-negative integers. 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

题目大致描述:

提供两个非空非负数的链表,将链表数字相加,返回新的链表。

解题思路:

1:为两个链表的数字求和得到新的数加入链表

2:数字超过10时进位

代码块(c#实现):

第一次提交代码(失败):

public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
ListNode answer = new ListNode(0);
ListNode result = answer;
int index = 0;
while(l1 != null || l2 != null){
int val1 = l1 != null?l1.val:0;
int val2 = l2 != null?l2.val:0;
int sum = val1+val2+index;
index = sum/10;
result.next = new ListNode(sum%10);
result = result.next;
l1 = l1.next;
l2 = l2.next;
}
if(index > 0) result.next = new ListNode(index);
return answer.next;
}

修改部分(提交成功):

if(l1 != null)l1 = l1.next;
if(l2 != null)l2 = l2.next;

心得:

1:为什么给val1、val2赋值时要判断是否为null?

2:如何进位和求得当前位置的值:在思考这个题目的时候,很容易就想到了用取余数来得到当前位的值,但是一时间并没有想到如何得到进位后的数值,想过用除法,但因为想的太狭隘,并没有很好的解决方法。

3:最后的一个条件判断?是用来判断当两个链表最后一个数字都已经加完时,是否有进位。

4:if和while的区别

声明:此为个人的学习看法,希望有其他看法的前辈,能够多多提点指教

LeetCode第二题的更多相关文章

  1. leetcode 第二题Add Two Numbers java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

  2. leetcode第二题--Median of Two Sorted Arrays

    Problem:There are two sorted arrays A and B of size m and n respectively. Find the median of the two ...

  3. LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表

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

  4. LeetCode第二题:Add Two Numbers

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

  5. LeetCode第二题—— Add Two Numbers(模拟两数相加)

    Description: You are given two non-empty linked lists representing two non-negative integers. The di ...

  6. Leetcode春季打卡活动 第二题:206. 反转链表

    Leetcode春季打卡活动 第二题:206. 反转链表 206. 反转链表 Talk is cheap . Show me the code . /** * Definition for singl ...

  7. LeetCode算法题-Subdomain Visit Count(Java实现)

    这是悦乐书的第320次更新,第341篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第189题(顺位题号是811).像"discuss.leetcode.com& ...

  8. LeetCode算法题-Rotate String(Java实现)

    这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 0 ...

  9. LeetCode算法题-Rotated Digits(Java实现)

    这是悦乐书的第316次更新,第337篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第185题(顺位题号是788).如果一个数字经过180度旋转后,变成了一个与原数字不同的 ...

随机推荐

  1. Hadoop启动脚本分析

    Hadoop启动脚本分析 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 能看到这篇博客的你估计对Hadoop已经有一个系统的了解了,最起码各种搭建方式你应该是会的,不会也没有关系, ...

  2. Docker:dockerfile自动构建镜像 [六]

    一.手动docker镜像的缺点 相对于手动制作的docker镜像,使用dockerfile构建的镜像有以下优点: 1.dockerfile只有几kb,便于传输 2.使用dockerfile构建出来的镜 ...

  3. SpringCloud笔记五:Feign

    目录 什么是Feign? 有了Ribbon我还要Feign干嘛? 新建consumer-feign 修改api项目 引入Maven文件 新建feign的接口 启动项目 报错 发泄发泄心情,一个段落 什 ...

  4. Java计算文件MD5值(支持大文件)

    import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.securit ...

  5. 第一节: Timer的定时任务的复习、Quartz.Net的入门使用、Aop思想的体现

    一. 前奏-Timer类实现定时任务 在没有引入第三方开源的定时调度框架之前,我们处理一些简单的定时任务同时都是使用Timer类, DotNet中的Timer类有三个,分别位于不同的命名空间下,分别是 ...

  6. tex 进度条

    \documentclass{beamer} \usepackage{tikz} \usetikzlibrary{calc} \definecolor{pbblue}{HTML}{0A75A8}% f ...

  7. 计算机视觉学习记录 - Implementing a Neural Network from Scratch - An Introduction

    0 - 学习目标 我们将实现一个简单的3层神经网络,我们不会仔细推到所需要的数学公式,但我们会给出我们这样做的直观解释.注意,此次代码并不能达到非常好的效果,可以自己进一步调整或者完成课后练习来进行改 ...

  8. filebeat_config

    Filebeat Prospector filebeat.prospectors: - input_type: log paths: - /var/log/apache/httpd-*.log doc ...

  9. 让CPU占用率曲线听你指挥

    使用GetTickCount()和Sleep(): Code#include <stdio.h> #include <unistd.h> #include <time.h ...

  10. 【原创】大叔问题定位分享(27)spark中rdd.cache

    spark 2.1.1 spark应用中有一些task非常慢,持续10个小时,有一个task日志如下: 2019-01-24 21:38:56,024 [dispatcher-event-loop-2 ...