2. Add Two Numbers【medium】
2. Add Two Numbers【medium】
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order 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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
解法一:
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode * dummy = new ListNode(INT_MIN);
ListNode * head = dummy;
int carry = ; while (l1 != NULL && l2 != NULL) {
int sum = l1->val + l2->val + carry;
carry = sum / ;
head->next = new ListNode(sum % );
head = head->next;
l1 = l1->next;
l2 = l2->next;
} while (l1 != NULL) {
int sum = l1->val + carry;
carry = sum / ;
head->next = new ListNode(sum % );
head = head->next;
l1 = l1->next;
} while (l2 != NULL) {
int sum = l2->val + carry;
carry = sum / ;
head->next = new ListNode(sum % );
head = head->next;
l2 = l2->next;
} if (carry) {
head->next = new ListNode(carry);
head = head->next;
} head->next = NULL; return dummy->next;
}
};
写得太长了,下面有短码的方法
解法二:
public class Solution {
/**
* @param l1: the first list
* @param l2: the second list
* @return: the sum list of l1 and l2
*/
public ListNode addLists(ListNode l1, ListNode l2) {
// write your code here
ListNode dummy = new ListNode();
ListNode tail = dummy; int carry = ;
for (ListNode i = l1, j = l2; i != null || j != null; ) {
int sum = carry;
sum += (i != null) ? i.val : ;
sum += (j != null) ? j.val : ; tail.next = new ListNode(sum % );
tail = tail.next; carry = sum / ;
i = (i == null) ? i : i.next;
j = (j == null) ? j : j.next;
} if (carry != ) {
tail.next = new ListNode(carry);
}
return dummy.next;
}
}
参考了九章的代码
解法三:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode preHead(), *p = &preHead;
int extra = ;
while (l1 || l2 || extra) {
int sum = (l1 ? l1->val : ) + (l2 ? l2->val : ) + extra;
extra = sum / ;
p->next = new ListNode(sum % );
p = p->next;
l1 = l1 ? l1->next : l1;
l2 = l2 ? l2->next : l2;
}
return preHead.next;
}
参考了@ce2 的代码
解法四:
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
// 题意可以认为是实现高精度加法
ListNode *head = new ListNode();
ListNode *ptr = head;
int carry = ;
while (true) {
if (l1 != NULL) {
carry += l1->val;
l1 = l1->next;
}
if (l2 != NULL) {
carry += l2->val;
l2 = l2->next;
}
ptr->val = carry % ;
carry /= ;
// 当两个表非空或者仍有进位时需要继续运算,否则退出循环
if (l1 != NULL || l2 != NULL || carry != ) {
ptr = (ptr->next = new ListNode());
} else break;
}
return head;
}
};
参考了九章的代码
2. Add Two Numbers【medium】的更多相关文章
- 167. Add Two Numbers【easy】
You have two numbers represented by a linked list, where each node contains a single digit. The digi ...
- 544. Top k Largest Numbers【medium】
Given an integer array, find the top k largest numbers in it. Example Given [3,10,1000,-99,4,100] ...
- 82. Remove Duplicates from Sorted List II【Medium】
82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...
- 75. Find Peak Element 【medium】
75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...
- 92. Reverse Linked List II【Medium】
92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...
- 61. Search for a Range【medium】
61. Search for a Range[medium] Given a sorted array of n integers, find the starting and ending posi ...
- 62. Search in Rotated Sorted Array【medium】
62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...
- 74. First Bad Version 【medium】
74. First Bad Version [medium] The code base version is an integer start from 1 to n. One day, someo ...
- 159. Find Minimum in Rotated Sorted Array 【medium】
159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...
随机推荐
- Hibernate4 No Session found for current thread
Hibernate4 与 spring3 集成之后, 如果在取得session 的地方使用了getCurrentSession, 可能会报一个错:“No Session found for curre ...
- 【AC自动机】zoj3228 Searching the String
对所有模式串建立AC自动机. 每个单词结点要记录该单词长度. 然后在跑匹配的时候,对每个单词结点再处理3个值,代表可重叠的匹配次数,不可重叠的匹配次数,以及“上一次不可重叠的匹配位置”,这样结合单词长 ...
- python3开发进阶-Web框架的前奏
我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web框架了. 1.自定义web框架 import socket ...
- 一个强大的UI node 抽象
基于cocos2d -x的一个强大的 界面对象的基类 ---@type uinode ui 对象的抽象 --@usage -- 界面打开的执行流程 -- 带*的是可选重写的函数,不带*的为必须实现的 ...
- Problem M: 输出九九乘法表
#include<stdio.h> int main() { int n,i,j; scanf("%d",&n); n>=&&n<= ...
- U盘安装Ubuntu 16.04出现:Failed to load ldlinux.c32
启动的时候如果不开启UEFI,则会提示: Failed to load ldlinux.c32 Boot failed: please change disks and press a key to ...
- 重新认识JavaScript里的创建对象
一.序 面向对象有一个标志,那就是它们都有类的概念,而通过类可以创建任意多个具有相同属性和方法的对象.ECMA-262把对象定义为“无序属性的集合,其属性可以包含基本值.对象或者函数”. 使用Obje ...
- VR开发者必看:4大最为值得关注的内容平台【转】
时间 2016-01-19 14:12:57 原文 http://www.sfw.cn/xinwen/478369.html 主题 虚拟现实 Oculus 对很多有意涉及VR行业的内 ...
- Ubuntu16.04安装Pytorch
一.安装 1. 官方github:https://github.com/pytorch/pytorch Install optional dependencies //安装依赖项 On Linux e ...
- N的阶层(王道)
题目描述: 输入一个正整数N,输出N的阶乘. 输入: 正整数N(0<=N<=1000) 输出: 输入可能包括多组数据,对于每一组输入数据,输出N的阶乘 样例输入: 4 5 15 样例输出: ...