# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None class Solution(object):
def addTwoNumbers(self, l1, l2):
l3 = ListNode()
current = l3
carry =
while l1 or l2: # , | None, | ,None
# Pad if None
if l1 is None:
l1v =
else:
l1v = l1.val
if l2 is None:
l2v =
else:
l2v = l2.val
# Sum
tmp = l1v + l2v + carry
if tmp >= :
x = tmp%
carry = int(tmp/)
else:
x = tmp
carry =
# Assign value
current.next = ListNode(x)
current = current.next
if l1 is not None:
l1 = l1.next
if l2 is not None:
l2 = l2.next
if carry != :
current.next = ListNode(carry)
return l3.next node1=ListNode()
node2=ListNode()
node3=ListNode() node1.next=node2
node2.next=node3 node4=ListNode()
node5=ListNode()
node6=ListNode() node4.next=node5
node5.next=node6 x=Solution()
print(x.addTwoNumbers(node1,node4).val)
print(x.addTwoNumbers(node1,node4).next.val)
print(x.addTwoNumbers(node1,node4).next.next.val)

输出


Add Two Numbers ,使用链表参数的更多相关文章

  1. Add Two Numbers - C++链表操作

    题目意思很简单,两个链表分别表示两个数,将两个数相加的结果存入一个新的链表中. 思路同样很简单:两个链表如果一样长,对应位置相加,如果某一个链表多了,则根据加的结果有无进位继续处理,全部结束后要考虑会 ...

  2. LeetCode 2. add two numbers && 单链表

    add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...

  3. LeetCode 2 Add Two Numbers(链表操作)

    题目来源:https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two ...

  4. 【LeetCode每天一题】Add Two Numbers(两链表相加)

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  5. LeetCode第[2]题(Java):Add Two Numbers (链表相加)——Medium

    题目难度:Medium 题目: You are given two non-empty linked lists representing two non-negative integers. The ...

  6. LeetCode OJ:Add Two Numbers (相加链表之数)

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  7. 445. Add Two Numbers II 链表中的数字求和

    [抄题]: You are given two non-empty linked lists representing two non-negative integers. The most sign ...

  8. 链表求和12 · Add Two Numbers

    反向存储,从左往右加 [抄题]: 你有两个用链表代表的整数,其中每个节点包含一个数字.数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头.写出一个函数将两个整数相加,用链表形式返回和.给 ...

  9. LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表

    题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...

  10. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

随机推荐

  1. <转>jmeter(四)HTTP请求

    本博客转载自:http://www.cnblogs.com/imyalost/category/846346.html 个人感觉不错,对jmeter讲解非常详细,担心以后找不到了,所以转发出来,留着慢 ...

  2. MyEclipse配置默认自带的XML代码格式化

    1.XML中的注释保持原样,不格式化为一行(Join lInes)内

  3. Java编译报错:意外的类型

    先上代码 后续更新 public class IncidentType { public static void main(String[] args) { int a = 3; int b = ++ ...

  4. MySQL笔记(八)存储过程练习&补充

    存储过程有什么优缺点?为什么要用存储过程?或者在什么情况下才用存储过程? 最直白的好处是存储过程比较快. 1.利用存储过程,给Employee表添加一条业务部门员工的信息. DROP PROCEDUR ...

  5. JDK常用命令(一)jps、jstat

    曾几何时,我们学习java都不再研究jdk而直接使用IDEA.eclipse和Netbeans,仿佛我们就认为我们的程序是这些编辑器编译出来的,这时多么可笑.殊不知,编辑器就是方便我们编辑开发的,而真 ...

  6. Codeforces Round #425 (Div. 2) Problem A Sasha and Sticks (Codeforces 832A)

    It's one more school day now. Sasha doesn't like classes and is always bored at them. So, each day h ...

  7. ERROR! The server quit without updating PID file (/application/mysql-5.6.40/data/db01-51.pid).

    centos7.5 安装mysql数据库报错 问题: [root@db01-51 scripts]# /etc/init.d/mysqld start Starting MySQL.Logging t ...

  8. 【做题】arc068_f-Solitaire——糊结论

    把所有数字放入双端队列后,结果大概是这样一个排列: \[P_1 1 P_2\] 其中\(P_1\)是递减序列,\(P_2\)是递增序列. 我们以\(1\)所在的位置\(k\)分割最终的排列\(A\). ...

  9. PL/SQL Developer几个使用小技巧

    1.选中sql语句的当前行 鼠标连续点击所在行3次. 2.记住登陆密码 工具 -> 首选项 -> Oracle -> 登录历史,勾选“带口令存储”. 3.查看Oracle的tnsna ...

  10. [CodeForces - 919B] Perfect Number

    题目链接:http://codeforces.com/problemset/problem/919/B AC代码: #include<cstdio> using namespace std ...