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 ...
随机推荐
- NOIP2018
非常糟糕.从未意识到过考场debuff这么严重. 果不其然,高档选手强如txc实力AK:而像我这样的中档选手就是重在考场发挥和自我调整了吧. 究竟要付出多少代价才能领会一个教训 看来要尝试更自闭一点
- 机器学习(一)之KNN算法
knn算法原理 ①.计算机将计算所有的点和该点的距离 ②.选出最近的k个点 ③.比较在选择的几个点中那个类的个数多就将该点分到那个类中 KNN算法的特点: knn算法的优点:精度高,对异常值不敏感,无 ...
- 10GNU C语言函数调用
6. C 函数调用机制概述 在 Linux 内核程序 boot/head.s 执行完基本初始化操作之后,就会跳转区执行 init/main.c 程序.那么 head.s 程序时如何把执行控制转交给 ...
- python爬虫基础11-selenium大全5/8-动作链
Selenium笔记(5)动作链 本文集链接:https://www.jianshu.com/nb/25338984 简介 一般来说我们与页面的交互可以使用Webelement的方法来进行点击等操作. ...
- bootmem_init_node
static unsigned long __init bootmem_init_node(int node, struct meminfo *mi) in arch/arm/mm/init.c 1. ...
- LeetCode(164)Maximum Gap
题目 Given an unsorted array, find the maximum difference between the successive elements in its sorte ...
- MySQL使用yum安装
1.下载mysql的repo源 $ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 2.安装mysql-comm ...
- Python之多线程与多进程(二)
多进程 上一章:Python多线程与多进程(一) 由于GIL的存在,Python的多线程并没有实现真正的并行.因此,一些问题使用threading模块并不能解决 不过Python为并行提供了一个替代方 ...
- zuul sample
https://github.com/kakawait/uaa-behind-zuul-sample/ 1.参考资料 项目:https://github.com/kakawait/uaa-behind ...
- day01_04.变量
变量的命名规则 变量名由字母小写a-z,大写A-Z,_下划线,数字0-9组成,php的变量名区分大小写;python的变量名也是区分大小写的 注意: PHP变量名必须以美元$符号开始; 变量名开头可以 ...