/**
* 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. 【剑指offer12】矩阵中的路径(回朔法),C++实现

    1.题目 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵中 ...

  2. 【javascript】js处理字符串

    javascript常用方法锦集: 处理字符串 在Javascript除了使用数组和对象 String.replace(regexp | replaceThis,replaceWith |callba ...

  3. grub2 设置Windows为默认启动系统

    1. 首先找到Windows的菜单menuentry.<blockquote># cat /boot/grub2/grub.cfg | grep Windows 结果: menuentry ...

  4. (转)Java获取CLASSPATH路径

    ClassLoader提供了两个方法用于从装载的类路径中取得资源: public URL getResource(String name); public InputStream getResourc ...

  5. CF1130E Wrong Answer

    E Wrong Answer 注意到 \(n\geq 2\) 时才可能有解,可以按如下方式构造一个 \(a_{1,2\dots n}\): 令 \(a_1=-1\) ,而后面的数都为正.记 \(s=\ ...

  6. Cookie用法

    //写入 protected void Button1_Click(object sender, EventArgs e) { HttpCookie cookie=new HttpCookie(&qu ...

  7. iis 部署 未在本地计算机上注册“Microsoft.Jet.OleDb.4.0”提供程序

    C#读取Access数据库在VS调试时正常,发布到win7-64的IIS之后报错“未在本地计算机上注册“Microsoft.Jet.OleDb.4.0”提供程序”.原因是VS调试时模拟的是32位,发布 ...

  8. ballerina 学习九 Client endpoints

    说白了就是连接外部服务的,可以是http jms websocket .... 简单例子 代码 import ballerina/http; import ballerina/log; endpoin ...

  9. python官网

    https://www.python.org/ https://docs.python.org/2/library/pydoc.html

  10. 安装QConfig备忘

    下载wget https://github.com/Qihoo360/QConf/archive/1.2.1.tar.gz 解压tar -zxf 1.2.1.tar.gz进入目录cd QConf-1. ...