【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 ...
随机推荐
- 关于java集合排序
对于排序,java开发者并不陌生. 为避免以后遗忘,现在再次总结一下! 常见8大排序算法, 平时自己熟悉的只有几种种!冒泡,二分/折半.插入.快排等!现在一一讲解一下,这里只讲思想,暂时不做实现! 一 ...
- android studio 导入外部库文件,以及将项目中module变成library引用依赖
一:导入如百度地图等的外部类. 步骤:1.首先 将androidstudio项目显示切换到 project 状态显示项目 2.然后添加.jar文件,将所有的.jar文件放入libs文件夹内(libs文 ...
- extjs+amcharts生成3D柱状图和数据表格使用总结
废话不多说,使用extjs+amcharts创建3d柱状图和数据表实例,如下: 1.首先定义一个数据模型 Ext.define("cacheHijack", { extend : ...
- sql数据导出导入格式化
SHELL脚本导入导出数据时,按逗号分隔,引号包含字段,null值'' gccli -uroot -h$ip -e"rmt:${sqlStr} into outfile '${name}${ ...
- VMware虚拟机服务的vmware-hostd自动启动和停止
安装了虚拟机 任务管理器会出现vmware-hostd.exe 占用了80端口,导致xampp打不开,所以就想关闭vmware,解决方案如下: 开始——运行——services.msc,找到VM打头 ...
- mozilla your firefox profile cannot be loaded. it may be missing or inaccessible
check the permissions ls -l ~/.cache | grep mozilla fix the permissions sudo chown -R $USER:$USER ~/ ...
- android 之 java环境部署
上甲骨文公司官网下载最新的jdk http://www.oracle.com/technetwork/cn/java/javase/downloads/jdk8-downloads-2133151-z ...
- laravel sum 多个字段
laravel中怎么实现下面的SQL select sum('profit'),sum('order_count') from pro where......; 参考 self::where('id' ...
- rune is alias of int32
I think chendesheng's quote gets at the root cause best: Go uses a lot of signed values, not just fo ...
- Linux 文件服务---------- nfs Server
Linux 文件服务nfs (Network file system)#网络文件系统 ---> 远程文件调用samba #文件共享(unix /linux /windows ) ,只能适用于局域 ...