(python)leetcode刷题笔记 02 Add Two Numbers
2018-01-03 19:39
661 查看
2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. 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.You may assume the two numbers do not contain any leading zero, except the number 0 itself
Example
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807.
# Definition for singly-linked list. #class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ if l1==None: return l2 if l2==None: return l1 carry=0 s=ListNode(0) ret=s while l1 or l2:#如果两个链表next均不为空 sum=0 if l1: sum+=l1.val l1=l1.next if l2: sum+=l2.val l2=l2.next sum+=carry s.next=ListNode(sum%10) s=s.next carry=(sum>=10) if carry==1: s.next=ListNode(1) del s return ret.next
ANSWER
相关文章推荐
- LeetCode 2 — Add Two Numbers(C++ Java Python)
- leetcode做题总结,题目Add Two Numbers /Add Binary 2011/11/01&2012/04/02
- LeetCode 2. Add Two Numbers (C++/python/js实现)
- 【leetcode刷题笔记】Add Two Numbers
- Leetcode 解题 Add Two Numbers Python
- [leetcode]-add two numbers @python
- LeetCode习题笔记——Add Two Numbers
- leetcode 2. Add Two Numbers python实现的理解,结点,单链表的应用,大数,进位的问题
- 【leetcode】2. Add Two Numbers(Python & C++)
- [leetcode]Add Two Numbers @ Python
- 【python】【leetcode】【算法题目2—Add Two Numbers】
- python写算法题:leetcode: 2. Add Two Numbers
- leetcode 【 Add Two Numbers 】 python 实现
- leetcode add two numbers python
- Leetcode 2. Add Two Numbers(python)
- [LeetCode]题解(python):002-Add Two Numbers
- LeetCode解题笔记2 Add Two Numbers
- 2.Add Two Numbers Leetcode Python New season for 2016
- 【LeetCode】【Python】【C++】2. Add Two Numbers代码实现