题目描述:

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. 1.5分布式通讯框架-RMI

    分布式通信框架-RMI讲解 什么是RPC Remote procedure call protocal RPC协议其实是一个规范.常用PRC框架:Dubbo.Thrif.RMI.Webservice. ...

  2. JS常用基础知识

    前言:在js中dom和bom是我们操作的基本,在最初接触时候我也懵,但是后来慢慢发现其实bom就是操作浏览器,而dom就是操作文本框节点.

  3. Angular记录(1)

    文档资料 箭头函数--MDN:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_fun ...

  4. PyQt5之布局管理

    目录 一 写在开头 1.1 本文内容 二 绝对布局 三 布局类 3.1 水平布局(QHBoxLayout)和垂直布局(QVBoxLayout) 3.2 水平布局和垂直布局实例 3.3 网格布局(QGr ...

  5. secureCRT免密码登陆Linux

    转自:http://blog.csdn.net/wangquannetwork/article/details/46062675 1.实现原理: 通过CRT生成的密钥对,把公钥上传到Linux服务器指 ...

  6. Java URLClassLoader 和 ClassLoader类加载器

    开始:看名字都带有ClassLoader,叫做类加载器,事实上是可以理解为动态的加载类,不过,也不是只能加载类,也可以加载其他形式的文件,比如说.properties属性文件. 区别:其实在两个类加载 ...

  7. IKanalyzer分词器分词并且统计词频

    <dependency> <groupId>com.janeluo</groupId> <artifactId>ikanalyzer</artif ...

  8. webservice访问的几种方式

    今天在对接的客户用到了webservice,最终采用wsimport生成本地代理方式以SDK的形式对接,但是想的完整的总结一下ws的几种调用方式. 发布服务的IP地址是:192.168.125.116 ...

  9. centos7环境下apache2.2.34的编译安装

    .获取apache2..34的源码包 http://archive.apache.org/dist/httpd/httpd-2.2.34.tar.gz .获取apache的编译参数 apache的编译 ...

  10. DataStructure-链表实现指数非递减一元多项式的求和

    // 2-链表实现多项式的求和.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<stdio.h> #inclu ...