2.Add Two Numbers Leetcode Python New season for 2016
来源:互联网 发布:java获取本机ip 编辑:程序博客网 时间:2024/06/06 13:25
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
# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ value_sum = 0 dummy_head = head = ListNode(None) while l1 or l2: if l1: value_sum += l1.val l1 = l1.next if l2: value_sum += l2.val l2 = l2.next head.next = ListNode(value_sum % 10) head = head.next value_sum /= 10 if value_sum == 1: head.next = ListNode(1) return dummy_head.next
traverse the whole tree and sum up the value in the node by order.
O(n) time and O(n) space
0 0
- 2.Add Two Numbers Leetcode Python New season for 2016
- 1. Two Sum Leetcode Python New season for 2016
- 4. Median of Two Sorted Arrays leetcode python New season for 2016
- 21. Merge Two Sorted Lists leetcode Python 2016 new Season
- 29. Divide Two Integers leetcode python new season 2016
- 34. Search for a Range leetcode Python 2016 new season
- 2.Add Two Numbers Leetcode Python
- [Leetcode]2.Add Two Numbers @python
- LeetCode 2. Add Two Numbers python 解法
- [leetcode:python] 2.Add Two Numbers
- leetcode 2. Add Two Numbers python
- [leetcode]-add two numbers @python
- 【Leetcode】【python】Add Two Numbers
- [LeetCode]2.Add Two Numbers
- LeetCode 2.Add Two Numbers
- LeetCode --- 2. Add Two Numbers
- [Leetcode] 2. Add Two Numbers
- 【leetcode】2. Add Two Numbers
- jquery源码学习3
- UIViewController中各方法调用顺序及功能详解
- Asp.net MVC中Html.Partial, RenderPartial, Action,RenderAction 区别和用法
- Smali语法参照表
- Android自定义动画学习,实现左右摇摆动画
- 2.Add Two Numbers Leetcode Python New season for 2016
- Git常用命令
- ThreadPoolTaskExecutor的配置解释
- ObjectARX2015开发环境设置
- 十大滤波算法程序大全(精心整理版)
- AAPT命令解析
- python基础教程共60课-第47课面向对象1
- Java程序打包成jar包
- MongoDB基本管理命令