2. Add_Two_Number

用两个非空链表分别表示两个非负整数,链表的节点表示数字的位,链表头表示数字的低位,链表尾表示数字高位。求两个链表所表示数字的和。

比如:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { var over = 0
var head, tmp *ListNode
for l1 != nil && l2 != nil {
if head == nil {
tmp = new(ListNode)
head = tmp
} else {
tmp.Next = new(ListNode)
tmp = tmp.Next
}
tmp.Val = l1.Val + l2.Val + over
over = tmp.Val / 10
tmp.Val -= over * 10
l1 = l1.Next
l2 = l2.Next
} for l1 != nil {
if head == nil {
tmp = new(ListNode)
head = tmp
} else {
tmp.Next = new(ListNode)
tmp = tmp.Next
} tmp.Val = l1.Val + over
over = tmp.Val / 10
tmp.Val -= over * 10 l1 = l1.Next
} for l2 != nil { if head == nil {
tmp = new(ListNode)
head = tmp
} else {
tmp.Next = new(ListNode)
tmp = tmp.Next
} tmp.Val = l2.Val + over
over = tmp.Val / 10
tmp.Val -= over * 10 l2 = l2.Next
} if over != 0 {
if tmp == nil {
tmp = new(ListNode)
head = tmp
} else {
tmp.Next = new(ListNode)
tmp = tmp.Next
}
tmp.Val = over
} return head
}

Leetcode_2. Add_Two_Number的更多相关文章

  1. [LeetCode_2] Add Two Numbers

    LeetCode: 2. Add Two Numbers /** * Definition for singly-linked list. * struct ListNode { * int val; ...

  2. python 基础部分重点复习整理2

    把这里的题目争取刷一遍 博客记录 python的ORM框架peewee SQLAlchemy psycopg2 Django 在1 的基础上,重点突出自己以前没注意的,做到精而不杂!!! Python ...

  3. day3-课堂代码

    # a = ('哈哈', 'xixi', 'hehe') # print(a[0]) # print(a[0:2]) # # # 列表 # a = ['哈哈', 'xixi', 'hehe', 1, ...

随机推荐

  1. Java 常用IO流操作详解

    1.基本概念 IO:Java对数据的操作是通过流的方式,IO流用来处理设备之间的数据传输,上传文件和下载文件,Java用于操作流的对象都在IO包中. 2.IO流的分类 图示:(主要IO流) 3.字节流 ...

  2. BottomNavigationView的使用

    BottomNavigationView的使用 废话少说, 先看东西 依赖 implementation 'com.android.support:design:26.1.0' 布局 //用这个控件需 ...

  3. CSP 试题编号201803-1 Java实现

    import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner input ...

  4. pycharm多行注释

    选中需要注释的代码 ctrl+/ #首字母大写# test = 'alex'# v = test.capitalize()# print (v)## 字符串的加法# n1 = 'my '# n2 = ...

  5. JS の 套路 I ~~

    小扇在到新公司以后,发现.我的js都忘记了!! 下面总结一下在装配数据时候用到的一些小小的公式,希望像我这样的前端小菜B起到帮助叭叭叭叭叭~~~ I.查找想找到的 HTML 元素 * i.查找元素 v ...

  6. Unity各平台内置宏定义

    属性 方法 UNITY_EDITOR #define directive for calling Unity Editor scripts from your game code. UNITY_EDI ...

  7. git 设置只输入一次用户名和密码

    https方式每次都要输入密码,非常不爽 按照如下设置可只输入一次 记住密码(默认15分钟): git config --global credential.helper cache 自己定义时间(一 ...

  8. ssm多数据源的操作

    公司要求,需要使用两个数据库,一个mysql,一个oracle.所以需要配置两个数据库来进行操作. 1.首先,需要在jdbc.properties文件中将两个库的配置数据写入,不过一个写driver, ...

  9. 记一次MacBook Pro无法连接wifi网络修复

    解决方案: https://blog.csdn.net/kimbing/article/details/79321001 真的哭了 原因是插入了我的USB3.0拓展坞影响了wifi信号 不知道原理是啥 ...

  10. es6之扩展运算符 三个点(...)

    对象的扩展运算符理解对象的扩展运算符其实很简单,只要记住一句话就可以: 对象中的扩展运算符(...)用于取出参数对象中的所有可遍历属性,拷贝到当前对象之中 let bar = { a: 1, b: 2 ...