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 ...
随机推荐
- Choose unique values for the 'webAppRootKey' context-param in your web.xml files! 错误的解决
大意是Log4jConfigListener在获取webapp.root值时,被后一context的值替换掉了,所以要在各个项目的web.xml中配置不同的webAppRootKey值,随即在其中一个 ...
- lavarel 添加自定义辅助函数
Laravel 提供了很多 辅助函数,有时候我们也需要创建自己的辅助函数. 必须 把所有的『自定义辅助函数』存放于 bootstrap 文件夹中. 并在 bootstrap/app.php 文件的最顶 ...
- DeepFaceLab小白入门(1):软件简介!
简介 DeepFaceLab是一种利用深度学习识别和交换图片和视频中的人脸的工具 这是一个github上的开源项目,所有人都可以查看源代码也能免费使用.个人认为这个项目的最大优点就是安装超级简单,几乎 ...
- hdu-2553 N皇后问题(搜索题)
在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对于给定的N,求出有多少种合法的放置方法. Inpu ...
- ssh登陆之忽略known_hosts文件
在平时工作中,有时候需要SSH登陆到别的Linux主机上去,但有时候SSH登陆会被禁止,并弹出如下类似提示: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...
- InnoDB Redo Flush及脏页刷新机制深入分析
概要: 我们知道InnoDB采用Write Ahead Log策略来防止宕机数据丢失,即事务提交时,先写重做日志,再修改内存数据页,这样就产生了脏页.既然有重做日志保证数据持久性,查询时也可以直接从缓 ...
- SPOJ 375 树链剖分 QTREE - Query on a tree
人生第一道树链剖分的题目,其实树链剖分并不是特别难. 思想就是把树剖成一些轻链和重链,轻链比较少可以直接修改,重链比较长,用线段树去维护. 貌似大家都是从这篇博客上学的. #include <c ...
- 《小团团团队》第八次团队作业:Alpha冲刺
项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 实验十二 团队作业8:软件测试与Alpha冲刺 团队名称 小团团团队 作业学习目标 (1)掌握软件测试基础技术; (2)学 ...
- Selenium 报错:Element is not clickable at point
WebDriverException: unknown error: Element <td class="grid - select - input " stype=&qu ...
- AtCoder Beginner Contest 070
我好想有点思维江化,所以我想给这个丝毫没有问题的abc也写下 A - Palindromic Number Time Limit: 2 sec / Memory Limit: 256 MB Score ...