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

/**
* 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)
{
ListNode headNode(-);
ListNode *preNode = &headNode; int carry = ;
while(L1 != NULL || L2 != NULL)
{
const int value1 = (L1 == NULL) ? : L1->val;
const int value2 = (L2 == NULL) ? : L2->val;
L1 = (L1 == NULL) ? NULL : L1->next;
L2 = (L2 == NULL) ? NULL : L2->next; const int value = (value1 + value2 + carry) % ;
carry = (value1 + value2 + carry) / ;
preNode->next = new ListNode(value);
preNode = preNode->next;
}
if(carry == )
{
preNode->next = new ListNode(carry);
preNode = preNode->next;
}
return headNode.next;
}
};

Add Two Numbers ---- LeetCode 002的更多相关文章

  1. Add Two Numbers LeetCode Java

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

  2. Add two numbers [LeetCode]

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

  3. LeetCode 面试:Add Two Numbers

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

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

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

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

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

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

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

  7. LeetCode Add Two Numbers II

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

  8. leetcode 第二题Add Two Numbers java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

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

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

随机推荐

  1. CentOS所有下载

    简述 CentOS(Community Enterprise Operating System - 社区企业操作系统)是Linux发行版之一,它是来自于Red Hat Enterprise Linux ...

  2. ubuntu14.04LS中安装sogouPingyin

    简介: 安装了新版本的ubuntu我才发现itbus的拼音是双拼的  没法改变 所以就安装了搜狗拼音 , 好像这个和小企鹅输入法有点关系 首先: 在终端中输入: sudo add-apt-reposi ...

  3. 呆呆的io流输入输出的一些基础

    关于io流的File类,下面来码一些基础属性: 文件的属性: /* public String getName() 获取文件的名字 public boolean canRead() 判断文件是否可以读 ...

  4. 错误:Error:未定义标识符"_TCHAR"

    原因:缺少头文件 解决方案:添加一条 #include <tchar.h>

  5. ACTIVITI 表结构数据分析

    ACTIVITI ACT_RU_EXECUTION 表     这个表是工作流程的核心表,流程的驱动都和合格表有密切的关系. 一般来讲一个流程实例都有一条主线.如果流程为直线流程,那么流程实例在这个表 ...

  6. HtmlAgilityPack解析全国区号页面到XML

    需求:完成一个城市和区号的xml配置文件 处理思路:通过HtmlAgilityPack解析一个区号页面,生产xml文件 页面:http://www.hljboli.gov.cn/html/code.h ...

  7. ajax使用jquery的实现方式

    1.jquery的ajax方法. $("#ajaxbtn").click(function(){ $.ajax({ url:"json.do", beforeS ...

  8. 启动、关闭Service

    //获取程序界面中的start.stop两个按钮 start = (Button) findViewById(R.id.start); stop = (Button) findViewById(R.i ...

  9. http错误和异常处理,认证和代理设置

    http错误: import urllib.requestreq = urllib.request.Request('http://www.python.org/fish.html')try:urll ...

  10. 利用Nginx+Mono+Fastcgi代替IIS对Asp.Net进行反向代理

    Nginx的好处相信我不必多说了,它作为一个相当轻量级的开源Web 服务器以及反向代理服务器而深受欢迎.越来越多的公司已经对它产生兴趣,包括我们公司的许多部门,利用它进行负载均衡和资源管理,之前写过一 ...