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. 使用Ajax异步加载页面时,怎样调试该页面的Js

    前言-本人不是干前端的,所以有的名词不专业 在前端中,有时候会遇到这样的框架,http://172.17.11.151:8060/frontend/backend.html#1.html (通过解析U ...

  2. java中字节流与字符流的区别

    字节流 在I/O类库中,java.io.InputStream和java.io.OutputStream分别表示字节输入流和字节输出流,它们都是抽象类,不能实例化,数据流中的最小单位是字节,所以叫做字 ...

  3. oracle 常用函数【转】

    常用Oracle函数 SQL中的单记录函数 1.ASCII 返回与指定的字符对应的十进制数; SQL> select ascii('A') A,ascii('a') a,ascii('0') z ...

  4. docker 学习

    vim /usr/lib/systemd/system/docker.service ExecStart=/usr/bin/docker daemon --bip=172.18.42.1/16 --r ...

  5. 汽车ABS系统-第一周作业

    ABS系统也成防抱死系统(Anti-lock Braking System),由罗伯特·博世有限公司所开发的一种在摩托车和汽车中使用,它会根据各车轮角速度信号,计算得到车速.车轮角减速度.车轮滑移率: ...

  6. [Android Tips] 24. Gradle listing project dependencies

    ./gradlew app:dependencies or install this Android Studio Plugin https://github.com/rholder/gradle-v ...

  7. Caused by: java.sql.SQLException: ORA-00918: column ambiguously defined

    外层t.*的时候,可能发现重复字段,所以检查内层的sql是否出现了重复字段的查询.

  8. RabbitMq中的交换机

          Rabbitmq的核心概念(如下图所示):有虚拟主机.交换机.队列.绑定:                    交换机可以理解成具有路由表的路由程序,仅此而已.每个消息都有一个称为路由键 ...

  9. Socket与Http通信

    HTTP(超文本传输协议): HTTP是应用层协议,主要用于解决数据包装的问题. HTTP协议是建立在TCP协议之上的一种应用. HTTP连接最显著的特点是客户端发送的每次请求都需要服务器回送响应,在 ...

  10. Java比C++好在哪儿?

    1.内外局部变量,不允许重名,避免了C++的那种混淆. 2.语言层面支持多线程,大幅减少了线程同步所需的代码量. 3.匿名类,匿名函数,可以作为参数直接写在参数所需要的位置,而不需要在其它地方再定义实 ...