链表相加

You are given two linked lists representing two non-negative numbers. 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.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

题目意思:

给定两个链表,返回其相加的结果。注意链表是倒序的,即第一个节点是个位。

解题思路:

这个有点类似于大数加法,只不过大数加法用的是vector,可以求出长度,而链表则不行,而且链表还要小心空指针。

加法过程就是小学加法的那个过程,对应位相加并且加上上一位的进制。因为链表长短不一,所以当某一个链表结束而另外一个没结束时,需要让没结束的那个链表的每个节点继续和0相加即可。

这题没什么难度哈……

代码如下:

 class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
if(!l1)
return l2;
if(!l2)
return l1; ListNode *head = new ListNode();
ListNode *ret = head;
ListNode *p = head;
int nCarry = ; while(l1 && l2){
int a = l1->val;
int b = l2->val;
int c = a + b + nCarry;
nCarry = c / ;
// c % 10 new node
p->next = new ListNode(c%);
p = p->next;
l1 = l1->next;
l2 = l2->next;
}
//当l1还有剩余时,用l1里的节点继续和0加
while(l1){
int a = l1->val;
int b = ;
int c = a + b + nCarry;
nCarry = c / ;
p->next = new ListNode(c%);
p = p->next;
l1 = l1->next;
}
//当l2还有剩余时,用l2里的节点继续和0加
while(l2){
int a = l2->val;
int b = ;
int c = a + b + nCarry;
nCarry = c / ;
p->next = new ListNode(c%);
p = p->next;
l2 = l2->next;
}
//类似于 1 -> 9和 2 -> 1相加这种,需要新加一个值为1的节点。
if(nCarry){
p->next = new ListNode();
nCarry = ;
}
ret = ret->next;
delete head;
return ret;
}
};

【LeetCode练习题】Add Two Numbers的更多相关文章

  1. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  2. LeetCode:1. Add Two Numbers

    题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...

  3. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  4. LeetCode 面试:Add Two Numbers

    1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  5. LeetCode #002# Add Two Numbers(js描述)

    索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...

  6. [Leetcode Week15] Add Two Numbers

    Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...

  7. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

  8. [LeetCode] 2. Add Two Numbers 两个数字相加

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  9. LeetCode之Add Two Numbers

    Add Two Numbers 方法一: 考虑到有进位的问题,首先想到的思路是: 先分位求总和得到 totalsum,然后再将totalsum按位拆分转成链表: ListNode* addTwoNum ...

  10. LeetCode 2. add two numbers && 单链表

    add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...

随机推荐

  1. Single Number 解答

    Question Given an array of integers, every element appears twice except for one. Find that single on ...

  2. HTML5 Canvas Arc Tutorial

    HTML5 Canvas Arc Tutorial HTML5 Canvas Arc Tutorial  

  3. Oleg Sych - » Pros and Cons of T4 in Visual Studio 2008

    Oleg Sych - » Pros and Cons of T4 in Visual Studio 2008 Pros and Cons of T4 in Visual Studio 2008 Po ...

  4. hdu2208之搜索

    唉,可爱的小朋友 Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. $.cookie is not a function

    在调试网站的时候,用jquery获取cookie时,报错: $.cookie is not a function; 一般情况$.cookie is not a function;错误原因: 一.没有引 ...

  6. C#静态构造函数和析构函数片段化认知

    一.静态构造函数 一个类可以有静态构造函数,实现如下源代码.静态构造函数有以下特性: 1).静态构造函数不能有修饰符(润饰符) 2).静态构造函数不能有参数 3).不能被调用——在实例化类的时候,静态 ...

  7. poj 2446 (二分匹配)

    题意:除了所给的一些点外,问能不能用1*2的矩形覆盖所有的点,矩形间不能重叠. 思路:简单二分匹配,,,,,,, #include<stdio.h> #include<string. ...

  8. android之ListPreference的用法_PreferenceActivity用法

    首先,我们明确,preference是和数据存储相关的.        其次,它能帮助我们方便的进行数据存储!为什么这个地方一定要强调下方便的这个词呢?原因是,我们可以根本就不使用,我们有另外的N种办 ...

  9. 【转换模型+扫描线】【UVA1398】Meteor

    The famous Korean internet company nhn has provided an internet-based photo service which allows The ...

  10. Reverse Linked List II java

    public static ListNode reverseBetween(ListNode head, int m, int n) { ListNode pre=head,current=head, ...