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】的更多相关文章

  1. 167. Add Two Numbers【easy】

    You have two numbers represented by a linked list, where each node contains a single digit. The digi ...

  2. 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] ...

  3. 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 ...

  4. 75. Find Peak Element 【medium】

    75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. [CF441E]Valera and Number

    题意:给定$x,k,p$和一份伪代码,伪代码大致是循环$k$次,每次有$p\%$的概率把$x$乘$2$,有$(100-p)\%$的概率把$x$加$1$,问最后在二进制下$x$的末尾期望$0$个数 鸽了 ...

  2. 求小于10000的素数的个数 Exercise06_10

    /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:求小于10000的素数的个数 * */ public class Exercise06_10 { public static ...

  3. inline-block 前世今生(转)

    曾几何时,display:inline-block 已经深入「大街小巷」,随处可见 「display:inline-block; *display:inline; *zoom:1; 」这样的代码.如今 ...

  4. linux-统计文件中相同行的数量

    cat sorttest | sort | uniq -c sorttest文件内容如下

  5. JNI之数据类型

    1. JNIEnv 作用 JNIEnv 概念 : 是一个线程相关的结构体, 该结构体代表了 Java 在本线程的运行环境 ; JNIEnv 与 JavaVM : 注意区分这两个概念; -- JavaV ...

  6. 我是如何给discuz模板做语法高亮的/vs code/textmate

    本人一直做ASP.NET开发,近期接到任务要用Discuz开发一个社区,第一次接触PHP,PHP灵活的语法,天生的模块化,各种语法糖深深的震惊了我,我从内心深处感受到了PHP是最牛逼的语言!!! 好了 ...

  7. FSLib.Extension库

    FSLib.Extension库是一个用于.NET的扩展函数库,所提供的函数和方法均使用扩展方法引入,包含数以百计的用于日常编写程序时使用的扩展方法. http://www.fishlee.net/s ...

  8. Nice Messager隐私权政策

    重视用户的隐私.您在使用我们的服务时,我们可能会收集和使用您的相关信息.我们希望通过本<隐私政策>向您说明,在使用我们的服务时,我们如何收集.使用.储存和分享这些信息,以及我们为您提供的访 ...

  9. Laravel系列教程一:安装及环境配置

    免费视频教程地址https://laravist.com/series/laravel-5-basic 最近在SF上面看到越来越多的Laravel相关的问题,而作为一个Laravel的脑残粉,本来打算 ...

  10. msSQL使用表参数

    使用表参数 表变量(Table Parameters)可以将整个表数据汇集成一个参数传递给存储过程或SQL语句.它的注意性能开销是将数据汇集成参数(O(数据量)). 定义了一个表参数jk_users_ ...