【LeetCode】2.Add Two Numbers
首先想到的是走到其中一个链表的尽头,然后把剩余的链表中的值放入写的链表,返回,但是自己写的代码好长。
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
int g,s=,stmp;
struct ListNode *pl,*ptmp,*pi;
pl=NULL;
while(l1&&l2)
{
g=l1->val+l2->val;
g+=s;
stmp=g/;
g%=;
struct ListNode* ptmp=(struct ListNode *)malloc(sizeof(struct ListNode));
ptmp->next=NULL;
ptmp->val=g;
if(pl==NULL)
{
pl=ptmp;
pi=pl;
}
else
{
pi->next=ptmp;
pi=pi->next;
}
s=stmp;
l1=l1->next;
l2=l2->next;
}
if(l1==NULL)
l1=l2;
while(l1)
{
g=l1->val;
g+=s;
stmp=g/;
g%=;
struct ListNode *ptmp=malloc(sizeof(struct ListNode));
ptmp->next=NULL;
ptmp->val=g;
pi->next=ptmp;
pi=pi->next;
s=stmp;
l1=l1->next;
}
if(s)
{
struct ListNode *ptmp=malloc(sizeof(struct ListNode));
ptmp->next=NULL;
ptmp->val=s;
pi->next=ptmp;
}
return pl;
}
Python:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
tmp = l1
up = 0
while l1 and l2:
l1.val += (l2.val+up)
if l1.val > 9:
up = 1
l1.val %= 10
else:
up = 0
p1,p2 = l1,l2
l1,l2 = l1.next,l2.next
if not l1 and not l2:
if up:
p1.next = ListNode(up)
return tmp
if l2:
p1.next = l2
l1 = p1.next
while l1 and up:
l1.val += up
if l1.val >9:
up=1
l1.val %= 10
else:
up = 0
p1 = l1
l1 = l1.next
if up:
p1.next = ListNode(up)
return tmp
【LeetCode】2.Add Two Numbers的更多相关文章
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- 【Leetcode】445. Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- 【LeetCode】002 Add Two Numbers
题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...
- 【LeetCode】2.Add Two Numbers 链表数相加
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- 【LeetCode】2. Add Two Numbers 两数相加
给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...
- 【LeetCode】165. Compare Version Numbers 解题报告(Python)
[LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】623. Add One Row to Tree 解题报告(Python)
[LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...
- 【LeetCode】989. Add to Array-Form of Integer 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组转整数再转数组 模拟加法 日期 题目地址:htt ...
- 【一天一道leetcode】 #2 Add Two Numbers
一天一道leetcode系列 (一)题目: You are given two linked lists representing two non-negative numbers. The digi ...
随机推荐
- GridView”的控件 必须放在具有 runat=server 的窗体标记内 “错误提示”
在做导出数据到EXCEL程序中,出现了错误提示:类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记 解决办法 重写 VerifyRendering ...
- Socket编程中 setsockopt的作用
功能描述: 获取或者设置与某个套接字关联的选 项.选项可能存在于多层协议中,它们总会出现在最上面的套接字层.当操作套接字选项时,选项位于的层和选项的名称必须给出.为了操作套接字层的选项,应该 将层的值 ...
- c# PictureBox 的图像上使用鼠标画矩形框
C# 中在图像上画框,通过鼠标来实现主要有四个消息响应函数MouseDown, MouseMove, MouseUp, Paint重绘函数实现.当鼠标键按下时开始画框,鼠标键抬起时画框结束. Poin ...
- 中兴电信光纤猫F450获取管理员密码方法
初衷:为了完成端口映射,一开始以为电信光猫不支持自定义路由,因为通过useradmin登录进去后没有找到对应的选项.一番了解之后,原来光猫有超级密码,电信装机时是不会告诉你的,电信客服一般也不会告诉你 ...
- openstack私有云布署实践【18 修改实例DHCP服务的DNS IP】
某天,由于Linux服务器默认没有DNS缓存功能,每次服务器每访问一个http域名链接时,都会触发一次DNS域名解析查询,降低了调用API接口的时延,所以我司后续启用的内网的dnsmasq DNS服务 ...
- 《C++反汇编与逆向分析技术揭秘》——流程控制语句的识别
if...else...语句 示例: if构成多分支语句 switch 有序线性的switch: 3E82D8位置存放了一个表,标明了要跳转到的地址: 这里的每四字节都标明的是每个case块的首地址: ...
- R安装
linux: 在编译R之前,需要通过yum安装以下几个程序: #yum install gcc-gfortran #否则报”configure: error: No F77 ...
- 1、API
基本API sectionsColor:['green','orange','red','grey'],//为每一层设置背景颜色 controlArrows:true,//是否显示幻灯片的左右按钮 v ...
- ligerUI实现分页
在公司实习看到公司框架里使用了ligerUI的grid进行分页,个人感觉挺好用的,自己摸索着实现了一遍记录下来 简单来说,liger grid 就是提交准备好的数据到指定的目标请求数据,拿到数据以后, ...
- centos 设置删除提示
centos 设置删除.覆盖提示 相信好多用linux的同学都用过cp这个命令来覆盖文件,但是新建的普通用户不会有任何提示,这时候心里就咯噔一下,赶紧看看新文件的是不是传上去了.如果加上提示,是不是就 ...