/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
Stack<ListNode> Q1 = new Stack<ListNode>();
Stack<ListNode> Q2 = new Stack<ListNode>(); while (l1 != null)
{
Q1.Push(l1);
l1 = l1.next;
} while (l2 != null)
{
Q2.Push(l2);
l2 = l2.next;
} var list = new List<ListNode>(); var step = ;
while (Q1.Count > || Q2.Count > )
{
var node1 = new ListNode();
if (Q1.Count > )
{
node1 = Q1.Pop();
} var node2 = new ListNode();
if (Q2.Count > )
{
node2 = Q2.Pop();
} var x = node1.val + node2.val + step;
if (x > )
{
step = ;
}
else
{
step = ;
} var node = new ListNode(x % );
list.Add(node);
}
if (step == )
{
list.Add(new ListNode());
} list.Reverse();
var head = list[];
var temp = head;
for (int i = ; i < list.Count; i++)
{
temp.next = list[i];
temp = list[i];
} return head;
}
}

https://leetcode.com/problems/add-two-numbers-ii/#/description

补充一个python的实现:

 class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
stack1 = self.buildStack(l1)
stack2 = self.buildStack(l2)
head = ListNode(-)
carry =
while len(stack1) > or len(stack2) > or carry != :
x = if len(stack1) == else stack1.pop(-)
y = if len(stack2) == else stack2.pop(-)
temp = x + y + carry
carry = if temp <= else
temp = temp %
node = ListNode(temp)
node.next = head.next
head.next = node
return head.next def buildStack(self,node):
stack = []
while node != None:
stack.append(node.val)
node = node.next
return stack

leetcode445的更多相关文章

  1. 【LeetCode445】 Add Two Numbers II★★

    题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...

  2. [Swift]LeetCode445. 两数相加 II | Add Two Numbers II

    You are given two non-empty linked lists representing two non-negative integers. The most significan ...

  3. leetcode445 Add Two Numbers II

    """ You are given two non-empty linked lists representing two non-negative integers. ...

  4. LeetCode链表解题模板

    一.通用方法以及题目分类 0.遍历链表 方法代码如下,head可以为空: ListNode* p = head; while(p!=NULL) p = p->next; 可以在这个代码上进行修改 ...

  5. LeetCode 445. 两数相加 II(Add Two Numbers II)

    445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个 ...

随机推荐

  1. AX内部公司

    <object width="450" height="500" align="middle" id="reader&quo ...

  2. margin top 无效

    常出现两种情况: (一)margin-top失效 两个层box1和box2,box1具有浮动属性,box2没有,这时候设置box2的上边距margin-top没有效果. 解决办法: 1.box2增加f ...

  3. Map集合学习

    Java中常用的Map实现类主要有:HashMap.HashTable.TreeMap.LinkedHashMap. 一:HashMap HashMap介绍 HashMap的底层其实是“链表的数组”, ...

  4. BZOJ3489 A simple rmq problem 【可持久化树套树】*

    BZOJ3489 A simple rmq problem Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一 ...

  5. HTTP错误类别

    http_status_bad_request (400) the request could not be processed by the server due to invalid syntax ...

  6. iOS 模态框覆盖导航栏

    1.使用window 覆盖 2.试图添加到 如果有一个场景:首页三个tab,要求只覆盖Navigation ,而不影响Tab使用,那么使用window 覆盖就不满足了.      这里我们可以使用如下 ...

  7. wamp配置局域网访问

    Apache的版本是2.4.9. <Directory "D:/wamp/www/">    #    # Possible values for the Option ...

  8. 【DUBBO】 Dubbo原理解析-Dubbo内核实现之基于SPI思想Dubbo内核实现

    转载:http://blog.csdn.net/quhongwei_zhanqiu/article/details/41577235 SPI接口定义 定义了@SPI注解 public @interfa ...

  9. WPF 使用MahApps.Metro UI库

    在WPF中要想使用Metro风格是很简单的,可以自己画嘛.. 但是为了节省时间,哈,今天给大家推荐一款国外Metro风格的控件库. 本文只起到抛砖引玉的作用,有兴趣还是推荐大家上官网,Thanks,官 ...

  10. 两分钟学会Android平台NDK编程(无须Eclipse和cygwin,可使用命令行打包多个so)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/wangbin_jxust/article/details/37389383 之前在进行cocos2d ...