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 ...
随机推荐
- 【概率dp 高斯消元】bzoj3270: 博物馆
一类成环概率dp的操作模式 Description 有一天Petya和他的朋友Vasya在进行他们众多旅行中的一次旅行,他们决定去参观一座城堡博物馆.这座博物馆有着特别的样式.它包含由m条走廊连接的n ...
- windows使用批处理bat文件批量打开程序
windows命令行官网教程: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/wind ...
- 【Windows7注册码】
[文章转载自 http://www.win7zhijia.cn/jiaocheng/win7_19324.html] 一.神Key: KH2J9-PC326-T44D4-39H6V-TVPBY TFP ...
- ubuntu12.04安装wireshark
1 安装 $ sudo apt-get install wireshark 2 启动 $ sudo wireshark 3 启动报错
- 逻辑与(&)和短路与(&&)的关系
逻辑与(&)和短路与(&&)在运算上对条件的结果判断不会产生影响,但会对条件判断的运算有影响.关键在于,逻辑与(&)在运算时会连续运算所有需要判断的命令.但短路与当遇到 ...
- HDU 2460 Network 边双连通分量 缩点
题意: 给出一个无向连通图,有\(m\)次操作,每次在\(u, v\)之间加一条边,并输出此时图中桥的个数. 分析: 先找出边双连通分量然后缩点得到一棵树,树上的每条边都输原图中的桥,因此此时桥的个数 ...
- configurationChanges
在Android中每次屏幕的切换动会重启Activity,所以应该在Activity销毁前保存当前活动的状态,在Activity再次Create的时候载入配置.在activity加上android:c ...
- Selenium WebDriver- 操作JavaScript的confirm弹窗
#encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webdriver i ...
- 大数据学习——scala数组
package com import scala.collection.mutable.ArrayBuffer /** * Created by Administrator on 2019/4/8. ...
- 九度oj 题目1457:非常可乐
题目描述: 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样 ...