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

Hide Tags

Linked List Math

 

  这题是链表遍历问题,容易处理。

#include <iostream>
using namespace std;
/**
* 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) {
if(l1==NULL) return l2;
if(l2==NULL) return l1;
int addOne=;
ListNode ret();
ListNode *p1=l1,*p2=l2,*pret=&ret;
while(){
if(p1==NULL&&p2==NULL&&addOne==) break;
if(p1==NULL&&p2==NULL){
pret->next = new ListNode((addOne)%);
pret = pret->next;
addOne = ;
continue;
}
if(p1==NULL){
pret->next = new ListNode((p2->val + addOne)%);
pret = pret->next;
addOne = (p2->val + addOne)/;
p2=p2->next;
continue;
}
if(p2==NULL){
pret->next = new ListNode((p1->val + addOne)%);
pret = pret->next;
addOne = (p1->val + addOne)/;
p1=p1->next;
continue;
}
pret->next = new ListNode((p1->val + p2->val + addOne)%);
pret = pret->next;
addOne = (p1->val + p2->val + addOne)/;
p1=p1->next;
p2=p2->next;
}
return ret.next;
}
}; int main()
{ return ;
}

[LeetCode] 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. Leetcode:Add Two Numbers分析和实现

    Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...

  4. [LeetCode] Add Two Numbers题解

    Add Two Numbers: You are given two non-empty linked lists representing two non-negative integers. Th ...

  5. LeetCode Add Two Numbers II

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

  6. LeetCode: Add Two Numbers 解题报告

    Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...

  7. 【LeetCode】2.Add Two Numbers 链表数相加

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

  8. leetcode 2 Add Two Numbers(链表)

    数字反过来这个没有什么麻烦,就是镜像的去算十进制加法就可以了,然后就是简单的链表. /** * Definition for singly-linked list. * struct ListNode ...

  9. [LeetCode]2. Add Two Numbers链表相加

    注意进位的处理和节点为null的处理 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int flag = 0; ListNode ...

随机推荐

  1. Python_装饰器、迭代器、生成器

    一.装饰器 装饰器的存在是为了实现开放封闭原则: 封闭: 已实现的功能代码块不应该被修改: 开放: 对现有功能的扩展开放. 理解装饰器的三要素: 函数的作用域 高阶函数 闭包 1. 闭包 闭包定义:如 ...

  2. 作业hashlib题目

    '''1.编写用户认证功能,要求如下 1.1.对用户密码加盐处理 1.2.用户名与密文密码存成字典,是以json格式存到文件中的 1.3.要求密用户输入明文密码,但程序中验证的是密文'''import ...

  3. 【CodeBase】PHP转换编码,读写文件/网页内容的防乱码方法

    核心代码: //检查字符串的编码 $charset=mb_detect_encoding($doc,['ASCII','GB2312','GBK','BIG5','UTF8'],TRUE); //字符 ...

  4. JZOJ 3388. 【NOIP2013模拟】绿豆蛙的归宿

    3388. [NOIP2013模拟]绿豆蛙的归宿 (Standard IO) Time Limits: 1000 ms  Memory Limits: 131072 KB  Detailed Limi ...

  5. VBA连接MySQL数据库以及ODBC的配置(ODBC版本和MySQL版本如果不匹配会出现驱动和应用程序的错误)

    db_connected = False '获取数据库连接设置dsn_name = Trim(Worksheets("加载策略").Cells(2, 5).Value)  ---- ...

  6. HDU 1535 S-Nim(SG函数)

    S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  7. itchat 动态注册

    动态注册时可以选择将itchat.run()放入另一线程或使用configured_reply()方法处理消息. 两种方法分别是: # 使用另一线程,但注意不要让程序运行终止 import threa ...

  8. Django基于Pycharm开发之三[LANGUAGE_CODE与TIME_ZONE]

    在django/conf/global_settings.py 中,我们可以找到关于language和timezone的通用配置信息,源码如下: # Local time zone for this ...

  9. Python 内置函数isinstance

    isinstance 用来判断对象的类型 isinstance(对象,类型/类型集合) 如果属于 返回True 不属于返回 False >>> a = 1 >>> ...

  10. WinRM和WinRS

    1.6.2 新远程工具:WinRM和WinRS 2012-05-14 10:18 张杰良 译 清华大学出版社 字号:T | T 综合评级: 想读(18)  在读(5)  已读(0)   品书斋鉴(0) ...