【Leetcode】【Medium】Add Two Numbers
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的更多相关文章
- LeetCode Linked List Medium 2. Add Two Numbers
Description You are given two non-empty linked lists representing two non-negative integers. The dig ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- C# 写 LeetCode Medium #2 Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...
- LeetCode 2. 两数相加(Add Two Numbers)
2. 两数相加 2. Add Two Numbers 题目描述 You are given two non-empty linked lists representing two non-negati ...
- (python)leetcode刷题笔记 02 Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...
- 【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 ...
- 【LeetCode每天一题】Add Two Numbers(两链表相加)
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
随机推荐
- WPF中设置了WindowStyle="None"后,窗口仍然有边框的解决方法
1. 设置了窗体的WindowStyle="None",窗口还是右边框,如下图: 2. 这是因为窗体默认是可以改变大小的,所以需要修改ResizeMode的值 ResizeMode ...
- centOS 7镜像文件下载
- CentOS虚拟机不能联网状况下yum方式从本地安装软件包
大家都知道yum是linux下一个非常好用的软件安装/卸载软件,它方便操作,而且最厉害的是可以解决令人头疼的包依赖关系.但是若是你的linux不能联网,若想使用yum安装软件,可以依照下面的方法. 1 ...
- java中equal和==的比较
equals 方法是 java.lang.Object 类的方法. 有两种用法说明: (1)对于字符串变量来说,使用“==”和“equals()”方法比较字符串时,其比较方法不同. “==”比较两个变 ...
- 【12】FtpWebRequest上传下载
下载文件 /// <summary> /// 下载文件 /// </summary> /// <param name="filename">&l ...
- [javaSE] IO流(装饰设计模式)
装饰设计模式:当想要对已有的对象进行功能增强时,可以自定义类将已有的对象传入,并提供加强功能,自定义的该类称为装饰类 典型的: Reader--FileReader --BufferedReader ...
- Spring中三个重要概念 IOC AOP Bean
Spring中三个重要概念 IOC AOP Bean 首先讲解一下Spring框架,以及为什么要使用Spring 框架? spring 是一个很好的容器框架, 是轻量级的IoC和AOP的容器框架,主要 ...
- 设计模式之状态模式IFORNOIF(二十二)
今天大风大雨, 但心情还行, 继续撸DOTA 状态模式(state pattern)定义 当一个对象的内在状态改变时允许改变其行为, 这个对象看起来像是改变了其类 这在我们开发的业务中太常见了, 角色 ...
- MySQL之多表查询练习
一.表格 表一 emp 表二 dept 表三 salgrade; 表四 年度利润表 二.习题 1. 查出至少有一个员工的部门.显示部门编号.部门名称.部门位置.部门人数. 2. 列出所有员工的姓名及 ...
- Windows任务计划向远程服务器拷贝文件,报登录失败: 未知的用户名或错误密码
问题产生很奇怪,当你登录到系统时,执行自动化作业是正常 但到了晚上凌晨自动执行作业时,则报登录失败: 未知的用户名或错误密码 解决方案: 在拷贝脚本中加及一行,创建net use 命名,每次文件拷贝前 ...