题目描述:

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. tensorflow Method源码阅读之 fully_connected

    https://www.tensorflow.org/api_docs/python/tf/contrib/layers/fully_connected fully_connected: 1.先根据权 ...

  2. Typora使用说明(记录总结)

    目录 区域元素 YAML FONT Matters 菜单 段落 标题 引注 序列 可选序列 代码块 数学块 表格 脚注 水平线 特征元素 链接 超链接 内链接 相关链 URLs 图片 斜体 加粗 删除 ...

  3. java.lang.OutOfMemoryError: Java heap space内存不足问题

    今晚,在定义一个new int[19001][13001]的数组时候内存不够:特转了一下方法: Exception in thread "main" java.lang.OutOf ...

  4. 第二节:深入剖析Thread的五大方法、数据槽、内存栅栏。

    一. Thread及其五大方法 Thread是.Net最早的多线程处理方式,它出现在.Net1.0时代,虽然现在已逐渐被微软所抛弃,微软强烈推荐使用Task(后面章节介绍),但从多线程完整性的角度上来 ...

  5. SpringBoot系列:Pojo validation

    JSR 303 规范了bean validation, Hibernate validator实现了JSR 303所有的规范, 同时也是最常用的validator 工具包. 使用 Hibernate ...

  6. SHELL希尔排序

    /****************************************************************************** * Compilation: javac ...

  7. metasploit 教程之信息收集

    信息收集 信息收集范围很大,可以从不同层面,不同维度进行信息收集. 系统补丁 我们知道目标机器缺少什么补丁就意味着存在与其对应的漏洞.我们可以利用这些漏洞来达到我们渗透攻击的目的. # 使用的模块 u ...

  8. wget无法正确下载jdk解决方案

    1 去官网复制下载链接 https://download.oracle.com/otn-pub/java/jdk/8u201-b09/42970487e3af4f5aa5bca3f542482c60/ ...

  9. pip 安装问题

    同时安装了Python2 和Python3的情况下,由于我的电脑默认的是使用Python3,pip的时候直接就安装在3上了,为了让2也安装,办法之一就是在安装python2的路径下比如,D:\Anac ...

  10. Vue2.0的三种常用传值方式、父传子、子传父、非父子组件传值

    参考链接:https://blog.csdn.net/lander_xiong/article/details/79018737