美文网首页
445. Add Two Numbers II

445. Add Two Numbers II

作者: 我是你的果果呀 | 来源:发表于2016-12-21 11:28 被阅读0次

You are given two linked lists representing two non-negative numbers. The most significant digit comes first 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.

Follow up: What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example: Input:(7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)    Output:7 -> 8 -> 0 -> 7

要求不能改动input, 也就是说要建立一个新的list, 第一种方法借用两个栈, 构建一个新的list, 这样空间复杂度高。

第二种方法就是借鉴+1 的记录最后一个不是9的位置, 

相关文章

网友评论

      本文标题:445. Add Two Numbers II

      本文链接:https://www.haomeiwen.com/subject/uekqvttx.html