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度旋转后,变成了一个与原数字不同的 ...
随机推荐
- Kubernetes之ServiceAccount
ServiceAccount 是什么 Service Account为Pod中的进程和外部用户提供身份信息.所有的kubernetes集群中账户分为两类,Kubernetes管理的serviceacc ...
- LeetCode 92. ReverseLinkedII
#include <iostream> using namespace std; struct ListNode { int val; ListNode *next; ListNode(i ...
- 第一节:框架前期准备篇之Log4Net日志详解
一. Log4Net简介 Log4net是从Java中的Log4j迁移过来的一个.Net版的开源日志框架,它的功能很强大,可以将日志分为不同的等级,以不同的格式输出到不同的存储介质中,比如:数据库.t ...
- Jasmine
Jasmine https://www.npmjs.com/package/jasmine The Jasmine Module The jasmine module is a package of ...
- 通俗易懂的vuex-demo
在main.js引入store.js
- Scrapy框架-----爬虫
说明:文章是本人读了崔庆才的Python3---网络爬虫开发实战,做的简单整理,希望能帮助正在学习的小伙伴~~ 1. 准备工作: 安装Scrapy框架.MongoDB和PyMongo库,如果没有安装, ...
- C# - 多线程(基础)
多线程 基础(Multithreading) 一些基本的关于线程和与其相关的概念 位)的变量赋值,这个操作就是原子性的.因为它可以一次性填充64位的二进制数据到栈上,属于一步完成,不会发生断裂.而假如 ...
- 一篇文让你看懂NB-IoT、LoRa、eMTC、Sigfox及ZigBee的应用场景【转】
转自:https://blog.csdn.net/nicholas_dlut/article/details/81051269
- Dos.Common
引言: Dos.Common是一个开发中的常用类库,如HttpHelper.LogHelper.CacheHelper.CookieHelper.MapperHelper等等.与Dos.WeChat. ...
- undefined reference to `pthread_create'
问题描述: ubuntu 16.04 下 C语言开发环境, 已经添加了头文件#include <pthread.h> 以及在Makefile中添加了 -lpthread,但是编译时仍然 ...