leetcode 2. Add Two Numbers python实现的理解,结点,单链表的应用,大数,进位的问题
来源:互联网 发布:人工智能需要哪些技术 编辑:程序博客网 时间:2024/06/06 00:02
题目:
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
题意:
给定两个单链表,每个单链表的每个节点存储一个非负整数。两个单链表数据相加,但是每个节点只能存储一个数字(即遇到10要进位),返回相加后的单链表。
代码:
class Solution:
def addTwoNumbers(self, l1, l2):
if l1 is None:
return l2
elif l2 is None:
return l1
else:
carry = 0
ret =ListNode(0)
ret_Last = ret
while(l1 or l2):
sum = 0
if(l1):
sum = l1.val
l1 = l1.next
if(l2):
sum += l2.val
l2 = l2.next
sum += carry
ret_Last.next = ListNode(sum%10)
ret_Last = ret_Last.next
carry = (sum >= 10)
if(carry):
ret_Last.next =ListNode(1)
ret_Last = ret.next
del ret
return ret_Last
- leetcode 2. Add Two Numbers python实现的理解,结点,单链表的应用,大数,进位的问题
- LeetCode 2. Add Two Numbers ***** 进位的巧妙解决
- Leetcode Add Two Numbers 遇到的问题
- LeetCode--Add Two Numbers的java实现
- Leetcode中Add Two Numbers算法的理解
- leetCode之Add Two Numbers python实现
- 明明的leetcode日常:2. Add Two Numbers
- LeetCode 2. Add Two Numbers (C++/python/js实现)
- 【LeetCode】【Python】【C++】2. Add Two Numbers代码实现
- 2. Add Two Numbers+链表的简单应用
- LeetCode 445. Add Two Numbers II--两个链表均按照由尾部到头部计算两个结点数值之和,保持进位
- leetcode 2---Add Two Numbers 进位 链表 数学
- 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
- 大数的加法 进位
- 面向对象
- dfs题:C - Prime Ring Problem
- 欢迎使用CSDN-markdown编辑器
- 排序算法再回顾
- Netty详解一(理解Netty的设计理念)
- leetcode 2. Add Two Numbers python实现的理解,结点,单链表的应用,大数,进位的问题
- 【数论】Disgruntled Judge, NWERC 2008, UVa12169
- UVA 11292 The Dragon of Loowater
- mysql中创建数据库并授权
- MPI并行计算与矩阵1
- UVA 1421 Archery(二分+维护区间)
- 网站被攻击常见的几种方式
- codeforces 724B Batch Sort
- Java基础复习---字符串