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 ...
随机推荐
- iOS-加载数据的实现-MJRefresh
使用CocoaPods加载三方库: pod 'MJRefresh' MJRefresh类结构图: 具体实现方法和效果图: The drop-down refresh 01-Default self.t ...
- Oracle添加自增长字段方法步骤
第一步:创建自增长序列 CREATE SEQUENCE ZH_ALARM_INFO_SEQ--自动增长列 INCREMENT BY 1 -- 每次加几个 START WITH 1 -- 从1开始计数 ...
- Visual Studio 2015 Preview 使用中问题一枚
只要碰到IO读写,文件不存在之类的系统异常,就会崩溃一下给你看看.直接重新VS. 不该有的问题确实存在着???? 正常情况是这样的 直接崩溃时万万不行的!!!!
- Aizu 2301 Sleeping Time(概率,剪枝)
根据概率公式dfs即可,判断和区间[T-E,T+E]是否有交,控制层数. #include<bits/stdc++.h> using namespace std; int K,R,L; d ...
- 版本管理工具-SourceSafe
一.什么是SourceSafe SourceSafe是Micrsoft公司推出的一款支持团队协同开发的配置管理工具,是Visual Studio的套件之一.因为其短小精悍,又继承了微软集成销售的一贯作 ...
- PAT (Basic Level) Practise (中文)- 1003. 我要通过!(20)
http://www.patest.cn/contests/pat-b-practise/1003 “答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入 ...
- Cannot read property 'tap' of undefined
E:\vue-project\vue-element-admin-master>npm run build:prod vue-element-admin@3.8.1 build:prod E:\ ...
- qsort()和bsearch()
qsort void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*)); Sort ...
- Java 替换word文档文字,指定位置插入图片
先说下 需要的依赖包 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ex ...
- 三、MySQL PHP 语法
MySQL PHP 语法 MySQL 可应用于多种语言,包括 PERL, C, C++, JAVA 和 PHP. 在这些语言中,Mysql在PHP的web开发中是应用最广泛. 在本教程中我们大部分实例 ...