当前位置:网站首页>算法--两数相加 II(Kotlin)
算法--两数相加 II(Kotlin)
2022-04-22 23:57:00 【小米科技Android 研发曹新雨】
题目:
给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。
你可以假设除了数字 0 之外,这两个数字都不会以零开头。
示例1:

输入:l1 = [7,2,4,3], l2 = [5,6,4]
输出:[7,8,0,7]
示例2:
输入:l1 = [2,4,3], l2 = [5,6,4]
输出:[8,0,7]
示例3:
输入:l1 = [0], l2 = [0]
输出:[0]
提示:
链表的长度范围为 [1, 100]
0 <= node.val <= 9
输入数据保证链表代表的数字无前导 0
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/add-two-numbers-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解决思路:
单链表倒叙的相加
借助栈可以方便的实现该算法
解决方法:
fun addTwoNumbers(l1: ListNode?, l2: ListNode?): ListNode? {
var result = ListNode(-1)
var cur = result
var l1Stack = LinkedList<Int>()
var l2Stack = LinkedList<Int>()
var resultStack = LinkedList<Int>()
var p1 = l1
var p2 = l2
while (p1 != null){
l1Stack.push(p1.`val`)
p1 = p1.next
}
while (p2 != null){
l2Stack.push(p2.`val`)
p2 = p2.next
}
var needAdd = false
while (!l1Stack.isEmpty() || !l2Stack.isEmpty() || needAdd){
var temp = 0
temp += if (!l1Stack.isEmpty()) l1Stack.pop() else 0
temp += if (!l2Stack.isEmpty()) l2Stack.pop() else 0
temp += if (needAdd) 1 else 0
needAdd = false
if (temp >= 10){
temp -= 10
needAdd = true
}
resultStack.push(temp)
}
while (!resultStack.isEmpty()){
cur.next = ListNode(resultStack.pop())
cur = cur.next
}
return result.next
}
版权声明
本文为[小米科技Android 研发曹新雨]所创,转载请带上原文链接,感谢
https://caoxinyu.blog.csdn.net/article/details/124356670
边栏推荐
- 2022-4-22 job MySQL single table query
- Common operations of PIL Library in deep learning
- Introduction to opencv (II) -- affine transformation and perspective transformation
- Typora style tuning
- 根据宽度计算文字大小
- Codeforce 1669 A and B
- 【leetcode】二叉树,654最大二叉树,105根据前序与中序遍历构造二叉树,106根据中序与后序遍列构造二叉树,889根据前序和后序遍历构造二叉树
- Introduction to cloud computing simulation framework cloudsim (screenshot version)
- 2022-04-22: give you a matrix board with the size of m x n to represent the deck, where each cell can be a warship 'x' or an empty space ', Returns the number of warships placed on the deck board. war
- Solve the error reporting problem of require is not defined
猜你喜欢

Write a beautiful login page with fluent (latest version)

浅谈LD_PRELOAD劫持
![[day22] - [linked list] intersecting linked list](/img/ce/703cd6a123019e9cda6eddadab6f2c.png)
[day22] - [linked list] intersecting linked list

Introduction to cloud computing simulation framework cloudsim (screenshot version)

ssh: connect to host 172.31.8.132 port 22: Connection refused

FPGA(四)数字IC面试的四个基本问题

Vscode uses emmylua plug-in to debug unity project tolua code

LabVIEW controls computer shutdown, hibernation, logout and restart

XPath positioning

Introduction to opencv (II) -- affine transformation and perspective transformation
随机推荐
Gets the range of the currently selected string
SystemVerilog verification - Test Platform preparation guide learning notes (5): function coverage
The parameter configuration of websoket package is out of the box
“亿”点点技术情怀
Interpretation of UVM source code, uvm-1.2 code review notes
Reg regular expression learning notes
STM32F103 independent watchdog
简便工作时间的法宝:以密钥验证的方式来登录服务器
80386 compilation_ Introduction to global description table GDT
Calculate text size based on height
"100 million" little technical feelings
字体自适应
SystemVerilog verification - Test Platform writing guide learning notes (3): connecting design and test platform
[leetcode] binary tree, maximum depth of 104 binary tree, diameter of 543 binary tree, maximum path and sum of 124 binary tree
【leetcode】二叉树,深入理解前中后序
合并两个有序的数组
Write a beautiful login page with fluent (latest version)
The latest MySQL tutorial is easy to understand
Day81 (dynamic programming, cross tree traversal)
No module named 'Django is reported when Django specifies the database_ test. settings‘