题目:

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

思路:计算进位,注意最后一位的处理。

#include<iostream>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
{
if(l1 == NULL || l2 == NULL)
{
return l1==NULL?l2:l1;
}
ListNode *head = l1; int len1 = 0;
int len2 = 0; ListNode *p1 = l1;
ListNode *p2 = l2; while(p1!=NULL)
{
len1++;
p1=p1->next;
}
while(p2!=NULL)
{
len2++;
p2=p2->next;
}
//cout<<len1<<";"<<len2<<endl;
ListNode *pre_l1 = l1;
ListNode *pre_l2 = l2; int jinwei = 0; while(l1!=NULL && l2!=NULL)
{
int sum = l1->val + l2->val + jinwei;
l1->val = sum%10;
//计算进位
if(sum>=10)
{
jinwei = 1;
}
else
{
jinwei = 0;
}
pre_l1 = l1;
l1 = l1->next;
pre_l2 = l2;
l2 = l2->next;
}
if(l1==NULL&&l2!=NULL)
{
pre_l1->next = l2;
while(l1==NULL&&l2!=NULL)
{
int sum = l2->val + jinwei;
l2->val = sum%10;
if(sum>=10)
{
jinwei = 1;
}
else
{
jinwei = 0;
}
pre_l2 = l2;
l2 = l2->next;
}
}
if(l2==NULL&&l1!=NULL)
{
while(l2==NULL&&l1!=NULL)
{
int sum = l1->val + jinwei;
l1->val = sum%10;
if(sum>=10)
{
jinwei = 1;
}
else
{
jinwei = 0;
}
pre_l1 = l1;
l1 = l1->next;
}
}
if(jinwei == 1 && len1>=len2)
{
ListNode *end = new ListNode(jinwei);
//找到最后的节点
pre_l1->next = end;
}
if(jinwei == 1 && len1<len2)
{
ListNode *end = new ListNode(jinwei);
//找到最后的节点
pre_l2->next = end;
}
return head;
}
int main(void)
{
//cout<<"l"<<endl;
ListNode *l1 = new ListNode(2);
ListNode *l2 = new ListNode(4);
ListNode *l3 = new ListNode(3);
l1->next = l2;
l2->next = l3;
ListNode *l11 = new ListNode(5);
ListNode *l21 = new ListNode(6);
ListNode *l31 = new ListNode(2);
l11->next = l21;
l21->next = l31; ListNode *p = addTwoNumbers(l1,l11);
while(p!=NULL)
{
cout<<p->val<<" ";
p=p->next;
}
delete l1;
delete l2;
delete l3;
delete l11;
delete l21;
delete l31;
system("pause");
return 0;
}

LeetCode_Add Two Numbers的更多相关文章

  1. leetcode——2

    1. 题目 Add Two Numbers You are given two linked lists representing two non-negative numbers. The digi ...

  2. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  3. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  4. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  5. [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  6. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  7. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  8. [LeetCode] Valid Phone Numbers 验证电话号码

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  9. [LeetCode] Consecutive Numbers 连续的数字

    Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...

随机推荐

  1. Django REST framework 知识点总结

    一.安装DjangoREST framework #先安装Django #安装必要的包 pip install djangorestframework coreapi (1.32.0+) - Sche ...

  2. 三篇很好的讲解keppalived的博客

    VRRP协议介绍 参考资料: RFC 3768 1. 前言 VRRP(Virtual Router Redundancy Protocol)协议是用于实现路由器冗余的协议,最新协议在RFC3768中定 ...

  3. Idea上配置btm

    1.  先在eclipse中配置好项目,再讲配置好的项目导入到idea中

  4. Android实现时间轴

    昨天群里有讨论时间轴的项目,没有接触过,以为非常吊,研究之后才知道表面都是忽悠人的,使用listview就能实现了,也没有什么新奇的东西 废话少说,直接上图 图片和文字都能够私人订制 没什么好说的,直 ...

  5. selenium基础框架的封装(Python版)这篇帖子在百度关键词搜索的第一位了,有图为证,开心!

    百度搜索结果页地址:https://www.baidu.com/s?ie=utf-8&f=3&rsv_bp=1&rsv_idx=1&tn=baidu&wd=se ...

  6. TCP/IP 在 Windows 下的实现

    Windows 实现TCP/IP 协议也是建立在上一篇博客的OSI 基础之上的. 用户态是由ws2_32.dll 和一些其他服务提供者的 dll 共同实现,当中ws2_32.dll 是一个框架.能够容 ...

  7. 你真的搞懂ES6模块的导入导出规则了吗

    前言 模块作为ES6规范的核心部分之一,在实际项目开发中经常会看到它的身影,那么我们是否真正了解它的相关规则呢,今天就带大家一起了解一下模块的导入导出规则 导入 ES6模块的导入(即import)大致 ...

  8. C语言位运算+实例讲解(转)

    按位或 按位与 按位异或 按位取反 左移右移 C语言位运算 有6种: &, | , ^(亦或), >(右移). 注意:参与位运算的元素必须是int型或者char型,以补码形式出现. 按位 ...

  9. LVS负载均衡服务

    LVS负载均衡服务 LVS负载均衡调度技术是在Linux内核中实现的,因此被称为Linux虚拟服务器.使用LVS时,不能直接配置内核中的ipvs,而需要使用ipvs的管理工具ipvsadm进行管理. ...

  10. LAMP集群项目五 nfs分发文件到服务器

    前边已经配置了免密钥登录,现在脚本直接调用scp即可 ./etc/init.d/functions ] then echo “argv is not correct” exit fi for ip i ...