【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 ...
随机推荐
- gulp备忘
// npm install gulp gulp-sourcemaps gulp-name gulp-notify del --save-dev // npm install gulp-ruby-sa ...
- php 内置函数 ( 随手能写出100个才算高级工程师 )
string 类型 去掉左右空格, trim()字符串的长度 strlen() float类型数组类型 count() 数组个数 资源类型 fopen() 大写转小写 strtolower()小写转大 ...
- JS 高效快速的数组去重
Array.prototype.uniquer = function() { var result = [], hash = {}; ; i < this.length; i++) { if ( ...
- ios UIImageView异步加载网络图片
方法1:在UI线程中同步加载网络图片 UIImageView *headview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 4 ...
- google 技巧
inurl: 用于搜索网页上包含的URL. 这个语法对寻找网页上的搜索,帮助之类的很有用. intext: 只搜索网页部分中包含的文字(也就是忽略了标题,URL等的文字). site: 可以限制你搜索 ...
- mac xcode 快捷键
shift + cmd + n 新建项目 cmd + n 新建文件 视图 option + cmd + 回车 打开助理编辑器 cmd + 回车 显示 ...
- 【微服务轻量化容器技术相关】同事分享的Docker学习汇总
还没时间去练习: Docker commands: docker images (list all image on this host) docker ps -a ( list all contai ...
- no method declared with objective-c selector error
down voteaccepted Swift 2.2 / Xcode 7.3 has a new way to use selector:Selector("funcName") ...
- suricata抓包方式之一AF_PACKET
suricata抓包方式之一AF_PACKET 噜拯渡 睦坚削 曜纡宄 式犒藿氆 咬焚桤φ 要蒯钮 喃俚夼 币噎嶂颐 话千叶舞就后悔了怎么想都容易让人引 虻谮м 及葚雏钏 看着表面平静实际 ...
- curl的简单使用步骤
要使用cURL来发送url请求,具体步骤大体分为以下四步: 1.初始化2.设置请求选项3.执行一个cURL会话并且获取相关回复4.释放cURL句柄,关闭一个cURL会话 // 1. 初始化一个cURL ...