leetcode 【 Add Two Numbers 】 python 实现
题目:
You are given two linked lists representing two non-negative numbers. 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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
代码:oj测试通过 Runtime: 171 ms
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @return a ListNode
def addTwoNumbers(self, l1, l2):
if l1 is None:
return l2
if l2 is None:
return l1 dummyhead = ListNode(0)
p = ListNode(0)
dummyhead.next = p jinwei = 0
while l1 is not None and l2 is not None:
curr_total = l1.val + l2.val + jinwei
l1 = l1.next
l2 = l2.next
curr_digit = curr_total % 10
jinwei = curr_total / 10
curr_node = ListNode(curr_digit)
p.next = curr_node
p = p.next if l1 is not None:
while l1 is not None:
curr_total = l1.val + jinwei
l1 = l1.next
curr_digit = curr_total % 10
jinwei = curr_total / 10
curr_node = ListNode(curr_digit)
p.next = curr_node
p = p.next
if l2 is not None:
while l2 is not None:
curr_total = l2.val + jinwei
l2 = l2.next
curr_digit = curr_total % 10
jinwei = curr_total / 10
curr_node = ListNode(curr_digit)
p.next = curr_node
p = p.next if jinwei == 1:
curr_node = ListNode(1)
p.next = curr_node return dummyhead.next.next
思路:
就是加法运算 注意两条链表上所有值计算过后 是否有进位;如果有进位 需要再处理一下。
leetcode 【 Add Two Numbers 】 python 实现的更多相关文章
- leetcode add two numbers python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- LeetCode: Add Two Numbers 解题报告
Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...
- Leetcode:Add Two Numbers分析和实现
Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...
- [LeetCode] Add Two Numbers题解
Add Two Numbers: You are given two non-empty linked lists representing two non-negative integers. Th ...
- Leetcode 解题 Add Two Numbers Python
原题: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- Leetcode2:Add Two Numbers@Python
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- [Leetcode] Add two numbers 两数之和
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
随机推荐
- nmon 工具的使用
原文链接:https://www.ibm.com/developerworks/cn/aix/library/analyze_aix/ 引言 nmon 工具可以为 AIX 和 Linux 性能专家提供 ...
- 【转/TCP协议编程】 基于TCP的Socket 编程
基于TCP(面向连接)的socket编程,分为客户端和服务器端. 客户端的流程如下: (1)创建套接字(socket) (2)向服务器发出连接请求(connect) (3)和服务器端进行通信(send ...
- C#访问修改符
修饰符可以指定访问的可见性,还可以指定其本质.(文章摘自<C#高级编程(第9版)>以及Microsoft) 1. 可见性修饰符 public:应用于所有类型或成员,即任何代码均可以访问该项 ...
- 使用tooltip显示jquery.validate.unobtrusive验证信息
通过重写CSS实现使用tooltip显示jquery.validate.unobtrusive验证信息,效果如图: 1. 在ViewModel中定义验证规则 [Display(Name = " ...
- Mysql数据库学习总结(一)
数据库概念 数据库(Database)是按照数据结构来组织.存储和管理数据,建立在计算机存储设备上的仓库. 简单说,数据库就是存放数据的仓库.和图书馆存放书籍.粮仓存放粮食类似. 数据库分类 分为 关 ...
- C++ vector容器类型的用法及注意
转自http://www.cnblogs.com/charley_yang/archive/2010/12/11/1903040.html vector类为内置数组提供了一种替代表示,与string类 ...
- ffmpeg —— 添加水印
1.添加水印——movie过滤器: ffmpeg -i inputfile -vf "movie=masklogo,scale= 60: 30[watermask]; [in] [wate ...
- 如何处理SAP HANA Web-Based Development Workbench的403 Forbidden错误
打开SAP云平台上的SAP HANA Web-Based Development Workbench超链接: 遇到错误信息:403 - Forbidden - The server refused t ...
- mysql 疑难问题-django
1不能存储中文 问题解决1: 确认表设计时,字段name_vn字符集是utf8,改成utf8后可以存储中文
- IBM中国
https://www.ibm.com/developerworks/cn/linux/l-memory/