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

本题特点:

1、链表已经是倒序的,因此首结点就是个位数字;

解题步骤:

1、建立preHead结点,指向新链表表头,新链表记录加法结果;(注意新建链表,不要在原链表上操作)

2、新建整形flag,记录进位;

3、开始循环操作,只要L1和L2有一个不为空,循环继续:

  (1)新建临时整形sum,初始值为flag;

  (2)L1不为空,则加上L1的值;L2不为空,则加上L2的值;

  (3)flag = sum / 10; sum = sum % 10;

  (4)记录在新建链表上;

4、如果flag还存在值,则再新建一个结点;

5、记录preHead->next地址head,delete preHead,返回head;

代码:

 /**
* 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* preheader = new ListNode();
ListNode* newlist = preheader;
int flag = ; while (l1 || l2) {
int sum = flag;
if (l1) {
sum += l1->val;
l1 = l1->next;
}
if (l2) {
sum += l2->val;
l2 = l2->next;
} flag = sum / ;
sum = sum % ;
newlist->next = new ListNode(sum);
newlist = newlist->next;
} if (flag)
newlist->next = new ListNode(flag); return preheader->next;
}
};

【Leetcode】【Medium】Add Two Numbers的更多相关文章

  1. LeetCode Linked List Medium 2. Add Two Numbers

    Description You are given two non-empty linked lists representing two non-negative integers. The dig ...

  2. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  3. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  4. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  5. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  6. C# 写 LeetCode Medium #2 Add Two Numbers

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

  7. LeetCode 2. 两数相加(Add Two Numbers)

    2. 两数相加 2. Add Two Numbers 题目描述 You are given two non-empty linked lists representing two non-negati ...

  8. (python)leetcode刷题笔记 02 Add Two Numbers

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

  9. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

  10. 【LeetCode每天一题】Add Two Numbers(两链表相加)

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

随机推荐

  1. MYSQL 缓存

    在PHP.INI中query_cache_type设置为1. 即 开始MYSQL全局SQL语句 都缓存.(0 不使用) 临时关闭查询缓冲的方法:1.    SELECT SQL_NO_CACHE fi ...

  2. 我爱Markdown (1)

    作为一个程序员,用Word, Excel等写技术文档实在是不那么方便.而我,作为一个Unix/Linux程序员,写技术文档还是喜欢用Wiki等在线写作工具.虽然Wiki已经很酷了,但跟Markdown ...

  3. POJ 1577 Falling Leaves 二叉搜索树

    HDU 3791 Falling Leaves 二叉搜索树  Figure 1Figure 1 shows a graphical representation of a binary tree of ...

  4. <机器学习实战>读书笔记--logistic回归

    1. 利用logistic回归进行分类的主要思想是:根据现有数据对分类边界线建立回归公式,以此进行分类. 2.sigmoid函数的分类 Sigmoid函数公式定义 3.梯度上升法    基本思想:要找 ...

  5. vue2.0读书笔记2-进阶

    一.深入响应式原理 二.过渡效果 三.过渡状态 四.Render函数 五.自定义指令 六.混合 七.插件 八.单文件组件 九.生产环境 十.路由 vue-router: http://router.v ...

  6. ora-01747:因为表中存在关键字造成的

    ORCLE报错解决(ora-01747:无效的用户.表.列,表.列)  一.ora-01747:无效的用户.表.列,表.列 这个问题出现是因为表中存在关键字造成的,如果想新增数据直接用sql语句,查询 ...

  7. SDSM框架

    标题解释 SDSM指的是SpringMVC+Dubbo+Spring+Mybatis的框架 ------------------------------------------------------ ...

  8. Ruby(2): 基本语法上

    表达式和变量: 这两点和其他主流的编程语言基本没有差别,这里直接跳过. 需要注意的是 ruby中 x=x+1 可以写成 x+=1 但是不支持 x++ , x-- 等一元运算符  比较运算符和表达式: ...

  9. iOS选择相片优化

    1.问题 在ios中有时需要选择本地图片或者拍照,有时候选择相片的时候会有多选和单选,所以需要首先封装相册选择,在之前的博客中也有写到IOS多选单选相册图片.这个只是对相册中选择图片的封装.我们在ap ...

  10. 微信小程序——动画操作时,rpx 和 px 的转换计算。

    嫌长版本: var rpx = 10000; var systemInfo = wx.getSystemInfoSync(); var px = rpx / 750 * systemInfo.wind ...