/**
* 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. MySQL的用户账户管理

    1.开启MySQL远程连接 1.sudo -i 2.cd /etc/mysql/mysql.conf.d/ 3.vim mysqld.cnf #bind-address = 127.0.0.1 把前面 ...

  2. AC自动机学习小结

    AC自动机 简要说明 \(AC\) 自动机,全称 \(Aho-Corasick\ automaton\) ,是一种有限状态自动机,应用于多模式串匹配.在 \(OI\) 中通常搭配 \(dp\) 食用. ...

  3. iOS 5 :一个UIPageViewController程序示例

    原文:http://www.techotopia.com/index.php/An_Example_iOS_5_iPhone_UIPageViewController_Application 在Xco ...

  4. Windows下安装Redis服务,修改查看密码,修改端口,常用命令

    一.安装 出自:https://jingyan.baidu.com/article/0f5fb099045b056d8334ea97.html 1.要安装Redis,首先要获取安装包.Windows的 ...

  5. @Autowired & @Resource 区别 & 解读@Bean

    一样     Autowired & @Resource 都可以用来Bean的注入,可以写在属性(字段)上.也可以写在setter方法上 不一样 1.来源不一样 @Autowired 由Spr ...

  6. java之反射概述

    类加载器和反射  类加载器: 1 类的加载过程: 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载.连接.初始化三步骤来实现对这个类进行初始化. 加载:就是指将class文件读入内存 ...

  7. c++重在运算符前置自增和后置自增

    class student { int age; }; int main() { class student stu; (stu++)++;//error ++(stu++);//error stu+ ...

  8. QT4.8.6静态编译

    下载源安装程序,http://download.qt.io/archive/qt/4.8/4.8.6/qt-everywhere-opensource-src-4.8.6.tar.gz 解压 cd 进 ...

  9. ***XX-net 和 proxyee-down

    看连接吧,留着方便自己查看 https://github.com/XX-net/XX-Net https://github.com/monkeyWie/proxyee-down/blob/master ...

  10. ubuntu 进入单用户模式

    进入单用户模式:  按shift进入 1.开机到grub时,用上下键移到第二行的恢复模式,按e(注意不是回车) 即Ubuntu,With Linux 3.2.0-23-generic(recovery ...