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 ...
随机推荐
- Java中遍历数组的三种方式复习
1 for循环遍历 通常遍历数组都是使用for循环来实现.遍历一维数组很简单,遍历二维数组需要使用双层for循环,通过数组的length属性可获得数组的长度. 程序示例: package captai ...
- 原生js 与 jQuery对比
1.原生JS与jQuery操作DOM对比 : https://www.cnblogs.com/QianBoy/p/7868379.html 2.比较jQuery与JavaScript的不同功能实 ...
- DNS排查技术图谱
# DNS排查技术图谱 ## 应用程序视角- 应用程序 - 浏览器 - hostname cache - ping- 操作系统 - hostname cache - 域名解析器 - dig domai ...
- 非JAVA客户端与mina使用 PrefixedStringCodecFactory 通讯
与C++,C#不同,java的写入字节顺序是从高到低(左低到右高) 例如 内存数据:{ 0x67,0x45,0x23,0x01} ,java int值是:0x6745231 而C++是:0x1234 ...
- linux开机故障解决方法
无法进grub方法1光盘或者网络引导进入Rescue切换原机系统 chroot /mnt/sysimage/ 安装grub grub-install /dev/sda 方法2 直接命令 grub-in ...
- ApiPost
模拟POST.Get 请求的工具----APIpost(中文版POSTMAN) 快速生成.一键导出api文档 在线模拟调试,结果实时返回 模拟登录后请求API 支持团队协作 模拟POST.Get 请求 ...
- (转) C#中使用throw和throw ex抛出异常的区别
通常,我们使用try/catch/finally语句块来捕获异常,就像在这里说的.在抛出异常的时候,使用throw和throw ex有什么区别呢? 假设,按如下的方式调用几个方法: →在Main方法中 ...
- SPOJ VFMUL - Very Fast Multiplication (FFT)
题目链接:VFMUL - Very Fast Multiplication Description Multiply the given numbers. Input n [the number of ...
- CentOS7 网卡配置文件解释
注:此网卡配置文件摘自CentOS7.4.1708系统 Linux 默认配置网卡的信息 TYPE=Ethernet 网卡类型:以太网 PROXY_METHOD=none 代理方式:关闭状态 BROWS ...
- Android四大组件之Service浅见
Service 是Android四大组件之一,可以在不显示界面的情况下在后台运行.还有一个作用是通过AIDL来实现进程间通信. Service的启动方式 Service的启动方式有两种,startSe ...