链表相加

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. [置顶] 玩转Eclipse — 自动注释插件JAutodoc

    代码注释是一种良好的编程习惯.不管对于他人还是自己,注释都有助于代码的阅读和理解.手动添加注释,是一个非常费时和繁琐的工作,严重影响软件开发效率,这也是绝大多数程序员不愿添加注释的主要原因.JAuto ...

  2. ajax jquery return没有返回值

    错误写法: function editdivisionmember(division_id,users_id){ $.ajax({ type:"POST", url:"/ ...

  3. 2014第一周五开发问题记URL传参乱码等

    今天修改了页面中URL传中文参数乱码问题,本来远离通过在tomcat中配置URIEncoder是可以解决所有乱码问题的,但怕以后有人下载一个新的tomcat然后直接把程序放里面运行然后再发现乱码问题而 ...

  4. Calculation(dfs+状压dp)

    Problem 1608 - Calculation Time Limit: 500MS   Memory Limit: 65536KB    Total Submit: 311  Accepted: ...

  5. socketpair的使用

    socketpair函数概要例如以下:#include <sys/types.h>#include <sys/socket.h>int socketpair(int domai ...

  6. apache 日志中记录代理IP以及真实客户端IP

    vim /usr/local/apach2/conf/httpd.conf 默认情况下log日志格式为:LogFormat "%h %l %u %t \"%r\" %&g ...

  7. asp.net中Repeart选中整行操作

    <asp:Repeater runat="server" ID="rpt_Student"> <HeaderTemplate> < ...

  8. java中List的用法

    list的添加删除等操作 import java.util.*; class TestList { public static void main(String[] args) { List<S ...

  9. Graham算法—二维点集VC++实现

    一.凸包定义 通俗的说就是:一组平面上的点,求一个包含所有点的最小凸多边形,这个最小凸多边形就是凸包. 二.Graham算法思想 概要:Graham算法的主要思想就是,最终形成的凸包,即包围所有点的凸 ...

  10. PL/SQL分页查询

    create or replace procedure fenye(tabelname in varchar2,currentpage in number,pageSize in number,inW ...