[Leetcode Week15] Add Two Numbers
Add Two Numbers 题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/add-two-numbers/description/
Description
You are given two non-empty linked lists representing two non-negative integers. 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.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
Solution
class Solution {
private:
    ListNode* copyList(ListNode* listHeadNode) {
        if (listHeadNode == NULL)
            return NULL;
        ListNode* resHeadNode = new ListNode(listHeadNode -> val);
        ListNode* resCurNode = resHeadNode;
        ListNode* listCurNode = listHeadNode -> next;
        while (listCurNode != NULL) {
            resCurNode -> next = new ListNode(listCurNode -> val);
            resCurNode = resCurNode -> next;
            listCurNode = listCurNode -> next;
        }
        return resHeadNode;
    }
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        if (l1 == NULL)
            return copyList(l2);
        if (l2 == NULL)
            return copyList(l1);
        bool isOverflow = false;
        int curVal = l1 -> val + l2 -> val;
        if (curVal >= 10) {
            isOverflow = true;
        }
        ListNode* resHeadNode = new ListNode(curVal % 10);
        ListNode* resPreNode = resHeadNode;
        ListNode* l1CurNode = l1 -> next;
        ListNode* l2CurNode = l2 -> next;
        while (l1CurNode != NULL && l2CurNode != NULL) {
            curVal = l1CurNode -> val + l2CurNode -> val;
            if (isOverflow)
                curVal++;
            resPreNode -> next = new ListNode(curVal % 10);
            isOverflow = (curVal / 10 > 0);
            resPreNode = resPreNode -> next;
            l1CurNode = l1CurNode -> next;
            l2CurNode = l2CurNode -> next;
        }
        if (l1CurNode == NULL)
            resPreNode -> next = copyList(l2CurNode);
        else
            resPreNode -> next = copyList(l1CurNode);
        if (isOverflow) {
            if (resPreNode -> next == NULL) {
                resPreNode -> next = new ListNode(1);
            } else {
                while (true) {
                    if (resPreNode -> next == NULL) {
                        resPreNode -> next = new ListNode(1);
                        break;
                    } else {
                        curVal = resPreNode -> next -> val + 1;
                        if (curVal >= 10) {
                            resPreNode -> next -> val = curVal % 10;
                            resPreNode = resPreNode -> next;
                        } else {
                            resPreNode -> next -> val = curVal;
                            break;
                        }
                    }
                }
            }
        }
        return resHeadNode;
    }
};
解题描述
这道题可以说解决的是数字相加的问题,但是由于数字的长度不确定,输入的数字可能是现有的内置数据类型无法存储的,所以像题目中给出的方法是,利用链表来记录数字,然后针对链表中的每一个节点的值进行相加。
明确了题目的要求之后,很快就可以清楚,题目最需要解决的问题就是链表边界和相加进位的问题。我的做法算是简单的对特殊情况打补丁。
[Leetcode Week15] Add Two Numbers的更多相关文章
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
		
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
 - LeetCode:1. Add Two Numbers
		
题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...
 - [LeetCode] 445. Add Two Numbers II 两个数字相加之二
		
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
 - LeetCode 面试:Add Two Numbers
		
1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
 - LeetCode #002# Add Two Numbers(js描述)
		
索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...
 - [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
		
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
 - [LeetCode] 2. Add Two Numbers 两个数字相加
		
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
 - LeetCode之Add Two Numbers
		
Add Two Numbers 方法一: 考虑到有进位的问题,首先想到的思路是: 先分位求总和得到 totalsum,然后再将totalsum按位拆分转成链表: ListNode* addTwoNum ...
 - LeetCode 2. add two numbers && 单链表
		
add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...
 
随机推荐
- Kubernetes初探 :总体概述及使用示例
			
Kubernetes是Google开源的容器集群管理系统.它构建于docker技术之上,为容器化的应用提供资源调度.部署运行.服务发现.扩容缩容等整一套功能,本质上可看作是基于容器技术的mini-Pa ...
 - BZOJ4299 Codechef FRBSUM(主席树)
			
感觉非常不可做,于是考虑有什么奇怪的性质. 先考虑怎么求子集和mex.将数从小到大排序,假设已经凑出了0~n的所有数,如果下一个数>n+1显然mex就是n+1了,否则若其为x则可以凑出1~n+x ...
 - python 内存分析
			
1.改源码重新编译打印相关信息 obmalloc.c 文件中打印 maxarenas,值为当前环境分配 arena 个数:分配 arena 时并没有马上分配对应的pools,故对于每一个 arena, ...
 - hdu 3339 In Action (最短路径+01背包)
			
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
 - Elasticsearch 插件head和kibana
			
本次安装在win7下,linux操作差不多. Elasticsearch的版本是6.5.1 一.前置条件 1.安装nodejs,如果已经安装了,检查一下版本,最好大于6以上,不然后面会失败,官网上已经 ...
 - HDOJ.1800 Flying to the Mars(贪心+map)
			
Flying to the Mars 点我挑战题目 题意分析 有n个人,每个人都有一定的等级,高等级的人可以教低等级的人骑扫帚,并且他们可以共用一个扫帚,问至少需要几个扫帚. 这道题与最少拦截系统有异 ...
 - apache出现You don't have permission to access / on this server. 提示
			
今天在新的linux上跑原来的代码,使用的虚拟主机的模式进行操作.几个相关的网站放在一个文件里,想法是通过网站列出的目录进行相应的网站进行操作.一切设置完成后,在浏览器中运行出现在You don't ...
 - Django CRM系统
			
本节内容 业务痛点分析 项目需求讨论 使用场景分析 表结构设计 业务痛点分析 我2013年刚加入老男孩教育的时候,学校就一间教室,2个招生老师,招了学生后,招生老师就在自己的excel表里记录一下,每 ...
 - ACE线程管理机制-并发控制(4)
			
转载于:http://www.cnblogs.com/TianFang/archive/2006/12/04/581857.html ACE Synchronization类 这一类并发控制对象一般也 ...
 - [lottery anayliser]lottery anayliser
			
抓取网页,获得获奖信息 #!/usr/bin/python import urllib2 import re import time def spider(url): ""&quo ...