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
反向存储,从左往右加 [抄题]: 你有两个用链表代表的整数,其中每个节点包含一个数字.数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头.写出一个函数将两个整数相加,用链表形式返回和.给 ...
随机推荐
- BZOJ5312 冒险(线段树)
记录区间and/or,修改时如果对整个区间影响都相同就打标记,否则递归.复杂度不太会证. #include<iostream> #include<cstdio> #includ ...
- 记一次java程序out of memory问题
在一个比较大批量的pdf转String项目中遇到了:java.lang.OutOfMemoryError: Java heap space错误 第一反应肯定是程序没有写好,大量循环时没有把程序中没有用 ...
- 3.11 - 3.12 A day with Google
补了一番游记. 找了一个本科学弟一起去上海游玩.本来老板还要我周一过去讨论寒假阅读的论文,总算是把讨论时间挪到周六了. 兴冲冲地买好车票后就开始期待上海Google office的神秘之旅. upda ...
- NOI前总结
最近也就是天天考试,总结一下. 7.1 开场T1T2都是不可做的概率期望,只有T3看起来可做,于是怒干4h+,将题解里面的所有结论都推出来了,大模拟写的一点毛病都没有,可还是因为2-SAT掌握不熟结果 ...
- 【linux之文件查看,操作,权限管理】
一.shell如何处理命令 1.shell会根据在命令中出现的空格字符,将命令划分为多个部分 2.判断第一个字段是内部命令还是外部命令 内部命令:内置于shell的命令(shell builtin) ...
- mac go2shell 安裝
配合Finder打开Finder,按住command键,拖动Go2Shell的图标到Finder菜单就可以在Finder快捷打开Go2Shell了
- string 中的一些优化事项
1.1 fmt vs "+" (无转义) import ( "testing" "fmt" ) var ( str = "he ...
- IOS计算文字高度
1.计算文字长度 NSString* str = @"你好"; .f; NSStringDrawingOptions options = NSStringDrawingUsesLi ...
- Java获取精确到秒的时间戳
1.时间戳简介: 时间戳的定义:通常是一个字符序列,唯一地标识某一刻的时间.数字时间戳技术是数字签名技术一种变种的应用.是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01 ...
- Java并发编程原理与实战四十五:问题定位总结
背景 “线下没问题的”. “代码不可能有问题 是系统原因”.“能在线上远程debug么” 线上问题不同于开发期间的bug,与运行时环境.压力.并发情况.具体的业务相关.对于线上的问题利用线上 ...