Add Two Numbers | LeetCode OJ

逻辑是简单的,主要是处理进位,以及两序列长度不一致的情况;

Python

# 链表结点定义
class ListNode(object):def __init__(self, x):self.val = xself.next = None# 为链表添加新的元素
def addTail(l, node):if l.next is None:l.next = nodeelse:p = l.nextwhile p.next:p = p.nextp.next = nodeclass Solution(object):def addTwoNumbers(self, l1, l2):carrier = 0x, y = l1.val, l2.valif x+y>=10:carrier = 1l = ListNode(x+y-10)else:l = ListNode(x+y)while l1.next and l2.next:x, y = l1.next.val, l2.next.valif x+y+carrier>=10:addTail(l, ListNode(x+y+carrier-10))carrier = 1else:addTail(l, ListNode(x+y+carrier))carrier = 0l1 = l1.nextl2 = l2.next# 以下两个 while 循环处理长度不一致的情况while l1.next:if l1.next.val+carrier >= 10:addTail(l, ListNode(l1.next.val+carrier-10))carrier = 1else:addTail(l, ListNode(l1.next.val+carrier))carrier = 0l1 = l1.nextwhile l2.next:if l2.next.val+carrier >= 10:addTail(l, ListNode(l2.next.val+carrier-10))carrier = 1else:addTail(l, ListNode(l2.next.val+carrier))carrier = 0l2 = l2.next# 还有进位if carrier == 1:addTail(l, ListNode(1))return l

leetcode(二)—— Add Two Numbers(Python/C++)相关推荐

  1. LeetCode之Add Two Numbers

    LeetCode之Add Two Numbers 题目:You are given two linked lists representing two non-negative numbers. Th ...

  2. LeetCode 445. Add Two Numbers II

    LeetCode 445. Add Two Numbers II Solution1:我的答案 利用了栈,这样就不用翻转链表了... /*** Definition for singly-linked ...

  3. 【注意】LeetCode 2. Add Two Numbers

    LeetCode 2. Add Two Numbers 这种沙比提怎么都写不对了??? Solution1:学习这种写法 /*** Definition for singly-linked list. ...

  4. LeetCode 445. Add Two Numbers II--面试算法题--C++,Python解法

    题目地址:Add Two Numbers II - LeetCode You are given two non-empty linked lists representing two non-neg ...

  5. LeetCode 2. Add Two Numbers--C++,Python解法--面试算法题

    题目地址:Add Two Numbers - LeetCode You are given two non-empty linked lists representing two non-negati ...

  6. [Leetcode] 445. Add Two Numbers II

    问题: https://leetcode.com/problems/add-two-numbers-ii/#/description 思路:该题与"415. Add Strings" ...

  7. leetcode 【 Add Two Numbers 】 python 实现

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  8. LeetCode:2. Add Two Numbers

    https://leetcode.com/problems/add-two-numbers/description/ 内容描述 You are given two non-empty linked l ...

  9. LeetCode 2. Add Two Numbers

    题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...

  10. leetcode 2. Add Two Numbers | 2. 两数相加(Java)

    题目 https://leetcode.com/problems/sum-of-two-integers/ 题解 需要注意 当两个链表不一样长时 的收尾处理. /*** Definition for ...

最新文章

  1. wfs.js 多路视频 串屏 闪烁
  2. 科普 | 以链接为中心的系统:Link-based Systems
  3. “中国好创意” CCF全国青年大数据创新大赛启动仪式 暨大数据大师论坛议程...
  4. jQuery模拟原生态App上拉刷新下拉加载效果代码
  5. Scala之flatMap操作
  6. 学习版origin申请安装
  7. Maya动画后期——粒子特效的制作
  8. 东大22春实用写作X《实用写作》在线平时作业2百分非答案
  9. android webview 广告过滤,使用webView屏蔽广告(实操版)
  10. 【DB笔试面试1-100】
  11. Java后端程序员都做些什么?
  12. TikTok印尼上线“TikTok Seller”;亚马逊禁用 Visa ;2021世界品牌500强出路...洞悉跨境
  13. 决策树模型——鸢尾花分类
  14. VisualSVN server下载安装
  15. Word段落前面有黑点 Word段落黑点怎么去掉
  16. 本地计算机上的windows installer,一个烦人的Windows Installer问题
  17. 什么是HTTP?为什么是不安全的?
  18. 题目53 停车场(ok)
  19. python+opencv将表格图片按照表格框线分割、识别
  20. 【从零开始的Java开发】1-4-3 多态:概念、实现、向上转型、向下转型、instanceof、类型转换、抽象类、抽象方法

热门文章

  1. java项目打成war包,使用ide打包部署在tomcat中
  2. Spark Conf配置用法
  3. 计算机网络拓扑分层,计算机网络和工程实践教程讲座主讲内容网络拓扑和分层体系结构.ppt...
  4. 如何在html上做弹框效果,网页|利用提示框(Tooltip)实现弹窗效果
  5. php 过滤非utf8,PHP如何将不是UTF8的字符过滤掉(代码)
  6. php $表达式,PHP表达式概念及实例详解
  7. 特征提取与检测(五) : LBP特征
  8. 新年第一天,人好多啊。
  9. 2019-08-12 计划与安排
  10. git学习(二):git config命令