02.Add Two Numbers
来源:互联网 发布:jquery获取form数据 编辑:程序博客网 时间:2024/06/09 16:53
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
python:
# Definition for singly-linked list.
# class ListNode:# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param {ListNode} l1
# @param {ListNode} l2
# @return {ListNode}
def addTwoNumbers(self, l1, l2):
out=p=ListNode((l1.val+l2.val)%10)
quot=(l1.val+l2.val)/10
while l1.next!=None and l2.next!=None:
l1=l1.next
l2=l2.next
q=ListNode((l1.val+l2.val+quot)%10)
p.next=q
p=p.next
quot=(l1.val+l2.val+quot)/10
while l1.next!=None:
l1=l1.next
q=ListNode((l1.val+quot)%10)
quot=(l1.val+quot)/10
p.next=q
p=p.next
while l2.next!=None:
l2=l2.next
q=ListNode((l2.val+quot)%10)
quot=(l2.val+quot)/10
p.next=q
p=p.next
if quot!=0:
q=ListNode(quot)
p.next=q
p=p.next
return out
0 0
- 02.Add Two Numbers
- Add two numbers & Add binary
- LeetCode: Add Two Numbers
- Add Two Numbers
- LeetCode: Add Two Numbers
- [LeetCode]Add Two Numbers
- LeetCode Add Two Numbers
- Leetcode: Add Two Numbers
- Add Two Numbers
- LeetCode : Add Two Numbers
- LeetCode Add Two Numbers
- [LeetCode] Add Two Numbers
- LeetCode4: Add Two Numbers
- Leetcode : Add Two Numbers
- Add Two Numbers
- 【leetcode】Add Two Numbers
- add two numbers
- Add Two Numbers
- 常见hash
- meta缓存(转载)
- IOS 屏幕旋转控制详解-特酷吧
- 有用网址
- java基础编程之查询某一年的二月有多少天(通过Calendar类)
- 02.Add Two Numbers
- C语言获取windows系统的相关信息
- Android MediaProvider--文件管理:必现,文件管理器中新建几个文件夹,批量删除后,连接电脑查看仍有部分文件夹未删除
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通) .
- 东软金沙笔试题目
- Android 自定义View-实现手写板
- NSUserDefaults写入和读取自定义 用户信息类
- MySQL中where条件的执行分析
- Ubuntu 14.04下安装/升级gcc 5.1.0