LeetCode第二题
题目描述:
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第二题的更多相关文章
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- 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 ...
- LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表
题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- LeetCode第二题:Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- LeetCode第二题—— Add Two Numbers(模拟两数相加)
Description: You are given two non-empty linked lists representing two non-negative integers. The di ...
- Leetcode春季打卡活动 第二题:206. 反转链表
Leetcode春季打卡活动 第二题:206. 反转链表 206. 反转链表 Talk is cheap . Show me the code . /** * Definition for singl ...
- LeetCode算法题-Subdomain Visit Count(Java实现)
这是悦乐书的第320次更新,第341篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第189题(顺位题号是811).像"discuss.leetcode.com& ...
- LeetCode算法题-Rotate String(Java实现)
这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 0 ...
- LeetCode算法题-Rotated Digits(Java实现)
这是悦乐书的第316次更新,第337篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第185题(顺位题号是788).如果一个数字经过180度旋转后,变成了一个与原数字不同的 ...
随机推荐
- Angular: 执行ng lint后如何快速修改错误
当我第一次被分配到“修正执行ng lint语句后的错误”这项任务前,我就被导师提前告知这是一个很无聊的任务,当我开始后,我发现其实有一些办法可以加快这个无聊单调的工作.接下来,我就分享一下我的经验. ...
- Centos7安装vsftpd (FTP服务器)
Centos7安装vsftpd (FTP服务器) 原文链接:https://www.jianshu.com/p/9abad055fff6 TyiMan 关注 2016.02.06 21:19* 字数 ...
- ElasticSearch 索引 剖析
ElasticSearch index 剖析 在看ElasticSearch权威指南基础入门中关于:分片内部原理这一小节内容后,大致对ElasticSearch的索引.搜索底层实现有了一个初步的认识. ...
- Beamer 中的页面链接
\documentclass[]{beamer} \usetheme{Madrid} \usenavigationsymbolstemplate{} \title{Main Title} \autho ...
- Centos7 安装 tree
Centos7 安装 tree 用命令 yum 安装 tree yum -y install tree
- Ubuntu 16.04 总出现红色圆圈警告和检测到系统程序出现问题
这种问题不可忽视!不可忽视!不可忽视!重要的事情说三遍!!!(一次死机,好多文件丢失,真是痛苦的经历) 自从从第三方安装了Python3.6,并将默认3.5改为3.6,导致ubuntu16.04右上角 ...
- 定义Sales_data类型
Sales_data初步定义如下: struct Sales_data { string bookNo; unsigned units_sold = ; double revenue = 0.0; } ...
- 软件测试之adb命令-实际公司使用场景--今日log
软件测试之adb命令-实际公司使用场景--今日log Dotest-董浩整理 1)可以看内存泄漏: 2)可以安装.卸载app--截图并提交bug: 3)可以通过抓app日志定位问题: 4)可以结合mo ...
- windows环境隐藏命令行窗口运行Flask项目
Linux下可以使用nohub来使Flask项目在后台运行,而windows环境下没有nohub命令,如何让Flask项目在windows中在后台运行而不显示命令行窗口呢? 1.写一个.bat脚本来启 ...
- Docker入门-安装(一)
Docker 在CentOS 7.0下安装Docker, CentOS 7.0默认使用的是firewall作为防火墙 查看防火墙状态 firewall-cmd --state 停止firewall ...