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. mysql 5.7初始化默认密码错误

    下载了一个mysql 5.7.17的安装包后,安装后怎么都启动不了,好在mysql安装是成功了,没办法只有使用命令行重新初始化设置了 我的mysql安装根目录为:C:\Program Files\My ...

  2. phpstrom怎样显示类的方法或函数列表

    phpstorm是能显示类的函数或方法列表的. 打开phpstorm,鼠标放到编辑器的右下角(矩形加一个下划线,跟电视机的图标差不多),不用点击就能显示出来一个弹窗: 让后点击Structure,就出 ...

  3. PHP操作redis的常用例子

    Redis常用的例子 1,connect 描述:实例连接到一个Redis. 参数:host: string,port: int 返回值:BOOL 成功返回:TRUE;失败返回:FALSE 示例: &l ...

  4. 09.1.VUE学习之watch监听属性变化实现类百度搜索栏功能ajax异步请求数据,返回数组

    09.1html里 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  5. 常用模块之 re shutil configparser hashlib xldt和xlwd

    shutil 高级文件处理模块 封装的更简单了 主要是文件的复制,移动,压缩解压缩 需要保证目标文件已经存在shutil.copymode('test.txt','testcopy4.txt') 压缩 ...

  6. Codeforces Round #524 (Div. 2) C. Masha and two friends 思路

    题目:题目链接 思路:直接计数显然是不好处理的,但分情况讨论只要不写错这题是一定可以出的,但这样基本做完这个题就没时间做其他题了,但当时我就这么蠢的这样做了,比赛一个半小时的时候突然发现一个似乎可行的 ...

  7. 使用python实现简单爬虫

    简单的爬虫架构 调度器 URL管理器 管理待抓取的URL集合和已抓取的URL,防止重复抓取,防止死循环 功能列表 1:判断新添加URL是否在容器中 2:向管理器添加新URL 3:判断容器是否为空 4: ...

  8. 菜鸟学Linux - 文件/文件夹的隐藏属性

    文件/文件夹居然还有隐藏属性?没错,隐藏属性对于文件/文件夹的安全很重要.好比如说,我们需要使用”鉴定符“来揭开装备的隐藏属性:在Linux中chattr/lsattr就是“鉴定符”. chattr基 ...

  9. Form 组件动态绑定数据

    1.Form 组件的作用: a.对用户提交的数据进行验证(form表单/ajax) b.保留用户上次输入的信息 c.可以生成html标签(input表单类的标签) 2..由于form组件中每个字段都是 ...

  10. python re 模块小结

    前言: 本人环境windows 7 64位,python2.7 re是什么: regular expression缩写,意为正则表达式,是python的众多模块之一 re用途: 从文本中有选择的批量抽 ...