Add Two Numbers I & II
Add Two Numbers I
You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.
Given 7->1->6 + 5->9->2. That is, 617 + 295.
Return 2->1->9. That is 912.
Given 3->1->5 and 5->9->2, return 8->0->8.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1; int value1 = , value2 = , carry = , sum = ;
ListNode head = new ListNode();
ListNode current = head; while (l1 != null || l2 != null || carry == ) {
value1 = (l1 == null ? : l1.val);
value2 = (l2 == null ? : l2.val);
sum = value1 + value2 + carry; current.next = new ListNode(sum % );
current = current.next; carry = sum / ;
l1 = (l1 == null) ? null : l1.next;
l2 = (l2 == null) ? null : l2.next;
}
return head.next;
}
}
Add Two Numbers II
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
分析: reverse 之后再相加,然后再reverse result linkedlist
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1; l1 = reverse(l1);
l2 = reverse(l2); return reverse(helper(l1, l2));
} public ListNode reverse(ListNode head) {
if (head == null || head.next == null) return head; ListNode pre = null;
ListNode current = head;
ListNode next = null; while (current != null) {
next = current.next;
current.next = pre;
pre = current;
current = next;
}
return pre;
} public ListNode helper(ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1; int value1 = , value2 = , carry = , sum = ;
ListNode head = new ListNode();
ListNode current = head; while (l1 != null || l2 != null || carry == ) {
value1 = (l1 == null ? : l1.val);
value2 = (l2 == null ? : l2.val);
sum = value1 + value2 + carry; current.next = new ListNode(sum % );
current = current.next; carry = sum / ;
l1 = (l1 == null) ? null : l1.next;
l2 = (l2 == null) ? null : l2.next;
}
return head.next;
}
}
方法二:用stack存list中的值,这样从上到下就是位数从小到大。
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
Stack<Integer> s1 = new Stack<>();
Stack<Integer> s2 = new Stack<>();
while (l1 != null) {
s1.push(l1.val);
l1 = l1.next;
}
while (l2 != null) {
s2.push(l2.val);
l2 = l2.next;
}
int carry = ;
ListNode head = new ListNode();
while (!s1.empty() || !s2.empty() || carry != ) {
int sum = ;
if (!s1.empty()) {
sum += s1.pop();
}
if (!s2.empty()) {
sum += s2.pop();
}
sum += carry;
carry = sum / ;
ListNode node = new ListNode(sum % );
node.next = head.next;
head.next = node;
}
return head.next;
}
}
Add Two Numbers I & II的更多相关文章
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode 445. 两数相加 II(Add Two Numbers II)
445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个 ...
- 445. Add Two Numbers II - LeetCode
Question 445. Add Two Numbers II Solution 题目大意:两个列表相加 思路:构造两个栈,两个列表的数依次入栈,再出栈的时候计算其和作为返回链表的一个节点 Java ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- 【LeetCode445】 Add Two Numbers II★★
题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 链表求和12 · Add Two Numbers
反向存储,从左往右加 [抄题]: 你有两个用链表代表的整数,其中每个节点包含一个数字.数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头.写出一个函数将两个整数相加,用链表形式返回和.给 ...
随机推荐
- java使用Cookie判断用户登录情况
1.判断是否登录 public boolean isLogin() { Set<Cookie> cookies = this.browser.getCookies(); String JS ...
- ansible操作(一)
ansible晋级操作之ad-hoc命令 所谓的ad-hoc命令! 如果我们敲入一些命令去比较快的完成一些事情,而不需要将这些执行的命令特别保存下来, 这样的命令就叫做 ad-hoc 命令.Ansib ...
- [BZOJ2095][Poi2010]Bridges 最大流(混合图欧拉回路)
2095: [Poi2010]Bridges Time Limit: 10 Sec Memory Limit: 259 MB Description YYD为了减肥,他来到了瘦海,这是一个巨大的海, ...
- Crawl(2)
http://cuiqingcai.com/3179.html # *-* coding: UTF-8 *-* import urllib2 import cookielib import re im ...
- C#基础-代码部署数据库及IIS站点
一.前言 最近忙里偷闲,做了一个部署数据库及IIS网站站点的WPF应用程序工具. 二.内容 此工具的目的是: 根据.sql文件在本机上部署数据库 在本机部署IIS站点,包括 ...
- 【BZOJ2281】【Sdoi2011】黑白棋 解题报告
[BZOJ2281][Sdoi2011]黑白棋 Description 小A和小B又想到了一个新的游戏. 这个游戏是在一个\(1\)*\(n\)的棋盘上进行的,棋盘上有\(k\)个棋子,一半是 ...
- Linux查看硬件信息命令
一.查看服务器硬件信息 (1)查看服务器型号.序列号 [root@Master ~]# dmidecode|grep "System Information" -A9|egrep ...
- 【bzoj1095】 ZJOI2007—捉迷藏
http://www.lydsy.com/JudgeOnline/problem.php?id=1095 (题目链接) 题意 一棵树,求最远的两黑点之间的距离,每次可以将黑点染白或者将白点染黑. So ...
- PHP获取IP地址的方法,防止伪造IP地址注入攻击
PHP获取IP地址的方法 /** * 获取客户端IP地址 * <br />来源:ThinkPHP * <br />"X-FORWARDED-FOR" 是代理 ...
- Python之旅:数据类型、字符编码、文件处理
一 引子 1 什么是数据? x=10,10是我们要存储的数据 2 为何数据要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同的类型的数据去表示 3 数据类型 以下每个类型都是有详细介绍链接的 ...