LeetCode OJ--Add Two Numbers
http://oj.leetcode.com/problems/add-two-numbers/
将用链表表示的两个数相加,(2 -> 4 -> 3) + (5 -> 6 -> 4) 就是342 + 465.刚开始把题目给理解错了,做复杂了。
主要是对指针的理解。
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(l1==NULL)
return l2;
if(l2==NULL)
return l1;
int sum;
int carry = ; ListNode *ans = l1;
ListNode *ans3 = ans;
//最后结果存到l1 ans
while(l1&&l2)
{
sum = l1->val + l2->val + carry;
carry = sum/;
if(carry)
l1->val = sum - ;
else
l1->val = sum;
l1 = l1->next;
l2 = l2->next;
}
if(l1==NULL &&l2==NULL)
{
if(carry == )
return ans; //找到最后一个节点
while(ans3->next)
ans3 = ans3->next;
ListNode *temp = new ListNode();
ans3->next = temp;
return ans;
}
//将两种情况化成一种
if(l1 == NULL)
{
while(ans3->next)
ans3 = ans3->next;
ans3->next = l2;
l1 = l2;
l2 = NULL;
} //要看最高位进位的情况
if(carry == )
return ans; while(carry == && l1)
{ l1->val += carry;
if(l1->val > )
{
l1->val = l1->val %;
carry = ;
l1 = l1->next;
}
else
return ans; }
if(carry == )
{
while(ans3->next)
ans3 = ans3->next;
ListNode *temp = new ListNode();
ans3->next = temp;
} return ans;
}
int main()
{
ListNode *l1 = new ListNode();
ListNode *n1 = new ListNode();
ListNode *n5 = new ListNode(); ListNode *l2 = new ListNode();
ListNode *n4 = new ListNode();
ListNode *n6 = new ListNode();
//ListNode *n8 = new ListNode(8);
l1->next = n1;
n1->next = n5;
l2->next = n4;
n4->next = n6;
//n6->next = n8;
Solution myS;
ListNode *temp = myS.addTwoNumbers(l2,l1);
return ;
}
囧,读复杂了,还做了个链表的翻转。
ListNode *reverse(ListNode * l2)
{
ListNode *n1,*n2,*before;
n1 = l2;
n2 = n1->next;
before = NULL;
while(n2)
{
n1->next = before;
before = n1;
n1 = n2;
n2 = n2->next;
}
n1->next = before;
return n1;
}
三个指针做链表的反转。
LeetCode OJ--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 Week15] Add Two Numbers
Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...
- [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 ...
随机推荐
- Optimization & Map
- 4.layhm框架初始化准备Init
hm\core\Boot 里 Boot 里run() 自动开起session 设置时区 <?php /** * Created by Haima. * Author:Haima * QQ:228 ...
- 科学计算库Numpy——numpy.ndarray
创建ndarray 元素类型 对于ndarray结构来说,里面所有的元素必须是同一类型的,如果不是的话,会自动的向下进行转换. 元素类型所占字节数 数组维数 元素个数 数组的维度 数组中填充固定值 索 ...
- Anaconda安装和环境的搭建
Anaconda安装 在官网上下载最新的Anaconda https://www.anaconda.com/distribution/ 我使用的是2018.12,Python 3.7这个版本的. 安装 ...
- Python9-网络编程3-day32
解决黏包的问题 #server import socket sk = socket.socket() sk.bind(('127.0.0.1',8080)) sk.listen() conn,addr ...
- mysql Plugin ‘InnoDB’ init function returned error
问题描述: 非正常关闭mysql,同时更改了my.cnf 导致启动时不支持innodb,出现如下错误: [ERROR] Plugin ‘InnoDB’ init function returned ...
- Ice cream samples Gym - 101670G 滑动扫描
题目:题目链接 思路:比赛中读错了题,题目要求选一个连续区间,却读成了随便选取几个柜台,英语要好好学啊,读懂题就很简单了,扫一遍就出结果了 AC代码: #include <iostream> ...
- (转)减少oracle sql回表次数 提高SQL查询性能
要写出高效的SQL,那么必须必须得清楚SQL执行路径,介绍如何提高SQL性能的文章很多,这里不再赘述,本人来谈谈如何从 减少SQL回表次数 来提高查询性能,因为回表将导致扫描更多的数据块. 我们大家都 ...
- debian使用ibus
$ sudo apt-get install ibus ibus-pinyin 点击右上角的键盘图标,设置拼音输入法
- emacs设置字体
* C-h f set-default-font set-default-font is an alias for `set-frame-font' in `frame.el'. (set-defau ...