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 对象的解释过程
- 清华集训2015-Day 2
校内测试做到了,于是就把解题报告发出来. 简单回路 一个 \(n\times m\) 的方格纸,有 \(k\) 个障碍点.\(q\) 次询问,每次询问 \((x,y)\) ,问有多少条简单回路经过 \ ...
- Eclipse中设置作者、日期等的方式
1.点击Windows->Preferences->Java->Code Style->Code Templates: 2.点击展开右侧的Comments选项卡,里面的选项对应 ...
- 修改grub需要修改权限命令
Ubantu每次更新都会把用于引导的grub文件重置,导致开机后,又找不到系统,看来以后还是要备份一下grub文件.另外,这周开始学习python,先学习Linux系统的命令行操作,很多操作是相通的. ...
- Java IO流学习总结 - BIO
Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据 ...
- What Kind of Friends Are You? ZOJ - 3960(ZheJiang Province Contest)
怎么说呢...我能说我又过了一道水题? emm... 问题描述: 给定 n 个待确定名字的 Friends 和 q 个问题.已知 c 个 Friends 的名字. 对于第 i 个问题,有 个 Fri ...
- BZOJ 4316: 小C的独立集 解题报告
4316: 小C的独立集 Description 图论小王子小C经常虐菜,特别是在图论方面,经常把小D虐得很惨很惨. 这不,小C让小D去求一个无向图的最大独立集,通俗地讲就是:在无向图中选出若干个点, ...
- windows 10 enterprise 企业版 mak激活密钥
企业版用户请依次输入: slmgr /ipk NPPR9-FWDCX-D2C8J-H872K-2YT43 slmgr /skms kms.xspace.in slmgr /ato
- bug3 乱码问题
出现乱码问题是因为各软件之间的编码方式不同导致 1.tomcat修改编码方法: 2.myeclipse中修改编码方式的方法: window----preference ----general----- ...
- CMake 案例
单个源文件 # CMake 最低版本号要求 cmake_minimum_required (VERSION 3.11) # 项目信息 project (Demo) # 指定生成目标 add_execu ...