002 Add Two Numbers 链表上的两数相加
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.
Example
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
详见:https://leetcode.com/problems/add-two-numbers/description/
Java实现:
/**
* 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 head=null;
ListNode pre=null;
int carry=0;
while(l1!=null||l2!=null){
int val1=l1!=null?l1.val:0;
int val2=l2!=null?l2.val:0;
int val=val1+val2+carry;
carry=val/10;
val%=10;
ListNode cur=new ListNode(val);
if(head==null){
head=cur;
}
if(pre!=null){
pre.next=cur;
}
pre=cur;
l1=l1!=null?l1.next:null;
l2=l2!=null?l2.next:null;
}
if(carry!=0){
ListNode l=new ListNode(carry);
pre.next=l;
}
return head;
}
}
python:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
head,pre,carry=None,None,0
while l1 or l2:
val1,val2=0,0
if l1:
val1=l1.val
l1=l1.next
if l2:
val2=l2.val
l2=l2.next
val=val1+val2+carry
carry=val//10
val%=10
cur=ListNode(val)
if not head:
head=cur
if pre:
pre.next=cur
pre=cur if carry:
pre.next=ListNode(carry) return head
002 Add Two Numbers 链表上的两数相加的更多相关文章
- Leetcode算法系列(链表)之两数相加
Leetcode算法系列(链表)之两数相加 难度:中等给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将 ...
- [CareerCup] 18.1 Add Two Numbers 两数相加
18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...
- [Swift]LeetCode2. 两数相加 | Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- [Swift]LeetCode445. 两数相加 II | Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- 【LeetCode-面试算法经典-Java实现】【002-Add Two Numbers (单链表表示的两个数相加)】
[002-Add Two Numbers (单链表表示的两个数相加)] 原题 You are given two linked lists representing two non-negative ...
- LeetCode 445. 两数相加 II(Add Two Numbers II)
445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个 ...
- LeetCode 2. 两数相加(Add Two Numbers)
题目描述 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入: ...
- LeetCode(2):Add Two Numbers 两数相加
Medium! 题目描述: 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头 ...
- LeetCode 2. 两数相加(Add Two Numbers)
2. 两数相加 2. Add Two Numbers 题目描述 You are given two non-empty linked lists representing two non-negati ...
随机推荐
- Maven(3)-利用intellij idea创建maven web项目
本文通过一个例子来介绍利用maven来构建一个web项目.开发工具:intellij idea. 一.新建maven项目 此处选择:Create from archetype.表示从已有的maven模 ...
- #include <deque>
deque \(deque\)头文件主要包括一个双端队列容器.是一个支持在两端插入两端删除的线性储存空间,与vector和queue相似.与\(vector\)比起来,\(deque\)可以在\(O( ...
- import module, from module import funtion区别
import module与from module import funtion区别: import module导入模块后你需要使用module.function()来调用一个函数 from mod ...
- MySQL数据库服务器参数优化mycnf,16G内存8核CPU,
业务场景: 后台支持手机在线更新系统,db服务器内存16G,8核,dell的pc服务器. qps: 200个左右 tps: 1个左右 一分钟50几个 sort_buffer_size = 32M 大了 ...
- POJ1365:质因数分解
Prime Land Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3590 Accepted: 1623 Descri ...
- HDOJ1150(最小点集覆盖)
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> ...
- macos下清除dnscache
sudo killall -HUP mDNSResponder 参见链接
- Linux下查看文件编码,文件编码格式转换和文件名编码转换
linux相关 2008-10-07 10:46 阅读1392 评论0 字号: 大大 中中 小小 如果你需要在Linux中 操作windows下的文件,那么你可能会经常遇到文件编 ...
- 【mybatis 的foreach的用法】
foreach一共有三种类型,分别为List,[](array),Map三种. foreach属性 属性 描述 item 循环体中的具体对象.支持属性的点路径访问,如item.age,item.inf ...
- Python中的变量,数据类型
Python中变量的命名规则:以字母和下划线开头,由字母,数字和下划线组成,区分大小写 Python中同样有加减乘除取余运算,还有一个运算符**,相当与幂运算,当然,幂运算的优先级要高于加减乘除 最后 ...