LeetCode 2. Add Two Numbers (两数相加)
题目标签:Linked List, Math
题目给了我们两个 Linked List, 各代表一个数字,不过顺序的反的。让我们把两个数字相加。
和普通的相加其实差不多,只不过变成了 Linked List, 还是要用到 / 和 %,具体看code。
Java Solution:
Runtime: 2ms, faster than 87%
Memory Usage: 44MB, less than 85%
完成日期:07/05/2019
关键点:利用 / 取得 carry;利用 % 取得 余数
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dummyHead = new ListNode(0);
ListNode cursor1 = l1;
ListNode cursor2 = l2;
ListNode cursor3 = dummyHead; int carry = 0; while(cursor1 != null || cursor2 != null) // go through both list
{
// if node exists, get the value, elsewise, default 0
int num1 = 0;
int num2 = 0; if(cursor1 != null)
num1 = cursor1.val;
if(cursor2 != null)
num2 = cursor2.val; int sum = num1 + num2 + carry; // update carry and sum
carry = sum / 10;
cursor3.next = new ListNode(sum % 10);
cursor3 = cursor3.next; // move both list to next node
if(cursor1 != null)
cursor1 = cursor1.next;
if(cursor2 != null)
cursor2 = cursor2.next;
} // at last, still need to check carry for last digit
if(carry == 1)
cursor3.next = new ListNode(1); return dummyHead.next;
}
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 2. Add Two Numbers (两数相加)的更多相关文章
- 【LeetCode】Add Two Numbers(两数相加)
这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ...
- [LeetCode]2.Add Two Numbers 两数相加(Java)
原题地址: add-two-numbers 题目描述: 给你两个非空的链表,表示两个非负的整数.它们每位数字都是按照逆序的方式存储的,并且每个节点只能存储一位数字. 请你将两个数相加,并以相同形式返回 ...
- [leetcode]2. Add Two Numbers两数相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- LeetCode(2):Add Two Numbers 两数相加
Medium! 题目描述: 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头 ...
- [CareerCup] 18.1 Add Two Numbers 两数相加
18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...
- Leetcode2.Add Two Numbers两数相加
给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...
- 【LeetCode】2. Add Two Numbers 两数相加
给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...
- LeetCode(2): 两数相加
本内容为LeetCode第二道题目:两数相加 # -*- coding: utf-8 -*- """ Created on Sun Mar 10 10:47:12 201 ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
随机推荐
- thinkphp 常量参考
预定义常量 预定义常量是指系统内置定义好的常量,不会随着环境的变化而变化,包括: URL_COMMON 普通模式 URL (0) URL_PATHINFO PATHINFO URL (1) URL_R ...
- 天梯L3-003. 社交集群——并查集
在社交网络平台注册时,用户通常会输入自己的兴趣爱好,以便找到和自己兴趣相投的朋友.有部分兴趣相同的人们就形成了“社交集群”.现请你编写程序,找出所有的集群. 输入格式: 输入的第一行给出正整数N(&l ...
- RF中滚动条的操作方法小结
滚动条分为俩种,一:主页面中的滚动条.二:页面中的子页面的滚动条. 每种滚动条有都分为上下滑动与左右滑动. 下面分别介绍: 一:主页面的滚动条上下滑动: execute javascript ...
- C语言中的数据类型转换函数
头文件#include<stdlib.h> 1. 函数名: atof 功 能: 把字符串转换成浮点数 用 法: double atof(const char *nptr); 2.函数名: ...
- 8.1_springboot2.x之Actuator应用监控
1.监管端点测试 引入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...
- C# WinfForm 控件之dev电子表格 SpreadSheet
网上找了一些资料可是不得入门 只能再回过头来看demos 看了一点 例子大多继承自SpreadsheetRibbonTutorialControlBase 这个类,它又继承自SpreadSheetTu ...
- USACO2012 Moo /// 模拟 oj21548
大致题意: 递归地描述序列:设S(0)为3个字符的序列“mo o”.然后在较长的序列小号(ķ)通过取序列的拷贝获得小号(ķ -1),则“摩... O”与ķ 2 O公司,然后该序列的另一个拷贝小号(ķ ...
- idea springboot 打包 war
1.pom文件中将项目改为 war
- ubuntu安装WPS替代office
安装 1.下载地址:http://community.wps.cn/download/(去WPS官网下载) 下载第一个即可 2.执行安装命令: sudo dpkg -i wps-office_10.1 ...
- 自记录:git如何上传文档到git@osc
前提: D盘有gitserver文件夹 双击桌面的git.exe文件,打开git命令窗口 输入cd d: 命令进入D盘 输入cd gitserver命令进入 找到git@osc自己参与项目里的htt ...