LeetCode: 2. Add Two Numbers

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *addTwoNumbers( ListNode *l1, ListNode *l2 )
{
int carry = 0;// 是否需要进位
ListNode * tail = new ListNode(0);
ListNode * ptr = tail;// 当前指针 while(l1 != NULL || l2 != NULL){
int val1 = 0,val2 = 0;
if (l1 != NULL){
val1 = l1 -> val;
l1 = l1 -> next;
}
if (l2 != NULL){
val2 = l2 -> val;
l2 = l2 -> next;
}
ptr -> val = (val1 + val2 + carry) % 10 ;
carry = ( val1 + val2 + carry )/ 10;
// 增加
if (l1 != NULL || l2 != NULL){
ptr -> next = new ListNode(0);
ptr = ptr -> next;
}
}
if (carry == 1){
ptr -> next = new ListNode(1);
}
return tail;
} };

[LeetCode_2] Add Two Numbers的更多相关文章

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

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

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

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

  3. 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]

    [本文链接] http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html [题目] 写一个函数,求两个整数的 ...

  4. Leetcode-2 Add Two Numbers

    #2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...

  5. [CareerCup] 2.5 Add Two Numbers 两个数字相加

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

  6. [LintCode] Add Two Numbers 两个数字相加

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

  7. LeetCode Add Two Numbers II

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...

  8. Two Sum & Add Two Numbers

    Two Sum 题目:https://leetcode.com/problems/two-sum/ class Solution(object): def twoSum(self, nums, tar ...

  9. No.002 Add Two Numbers

    Add Two Numbers Total Accepted: 160702 Total Submissions: 664770 Difficulty: Medium You are given tw ...

随机推荐

  1. 我喜欢ASP.NET的MVC因为它牛逼的9大理由(转载)

    我很早就关注ASP.NET的mvc的,因为最开始是学了Java的MVC,由于工作的原因一直在做.Net开发,最近的几个新项目我采用了MVC做了,我个一直都非常喜欢.Net的MVC.我们为什么使用MVC ...

  2. JPA 2.1 Coverter 注解

    @Converter(autoApply = true) public class VehicleConverter implements AttributeConverter<Vehicle, ...

  3. 点击链接跳转到固定div位置处(类似锚点链接)

    $('.joinbtn').click(function(){ var a = $("#contact").offset().top;$("html,body" ...

  4. setTimeout调用带参数的函数的方法

    function test(s){    alert(s);}window.setTimeout(function(){test('str');},1000);这样就可以了...为什么是这样呢.因为s ...

  5. Exec in Job and NewQuery

    1.背景 Job:一个步骤执行两个存储过程ProcA.ProcB.ProcA定义一个游标,从表TabA中检索数据,逐条插入到表TabB.如果某条数据不满足TabB上的约束(比如非空)导致插入失败.那么 ...

  6. tar压缩

    tar 压 缩:tar -jcv -f filename.tar.bz2 要被压缩的文件或目录名称 查 询:tar -jtv -f filename.tar.bz2 解压缩:tar -jxv -f f ...

  7. HUD 5050 Divided Land

    http://acm.hdu.edu.cn/showproblem.php?pid=5050 题目大意: 给定一个矩形的长和宽,把这个矩形分成若干相等的正方形,没有剩余.求正方形的边长最长是多少. 解 ...

  8. JS新API标准 地理定位(navigator.geolocation)/////////zzzzzzzzzzz

    在新的API标准中,可以通过navigator.geolocation来获取设备的当前位置,返回一个位置对象,用户可以从这个对象中得到一些经纬度的相关信息. navigator.geolocation ...

  9. bzoj3884 上帝与集合的正确用法

    a^b mod P=a^(b mod phi(p)) mod p,利用欧拉公式递归做下去. 代码 #pragma comment(linker,"/STACK:1024000000,1024 ...

  10. Super Jumping! Jumping! Jumping!

    Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. May ...