【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 ...
随机推荐
- 【LeeetCode】4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- gridview动态添加行(不用datatable实现)
GridViewRow newrow = , , DataControlRowType.Separator, DataControlRowState.Normal); TableCell[] tc=] ...
- Kafka集群搭建
1.zookeeper搭建 Kafka集群依赖zookeeper,需要提前搭建好zookeeper zookeeper快速搭建推荐地址:http://nileader.blog.51cto.com/1 ...
- 使用Stardict命令行版本sdcv
sdcv命令的常用选项如下: -l:列出安装的词典 -u:指定查词所用的词典 在我的电脑上列出的词典有: Dictionary's name Word count Merrian Webster 10 ...
- UGUI和现实世界的比例关系
之前测试过默认大小的 Cube 在现实中的 比例关系,得出基本单位为 m 的结论,至于 UGUI和现实世界的比例关系 看下图就知道了: Cube Collider 的大小: Button 的大小: 其 ...
- Linux连接xshell找不到IP信息
虚拟机环境下的Linux连接xshell的网络连接找不到eth0(IP)信息的解决方法 1 输入ifconfig,如果有eth0信息,直接填写eth0上面的IP信息 2 输入ifconfig ...
- CentOS6.5部署L2TP over IPSec
一.环境介绍: 1.CentOS 6.5 (要求双网卡做软路由,如果只是做VPN可以单网卡) a.外网IP: b.内网IP: 2.Window 10 主机一台做为一台内网测试软路由使用: a.内网IP ...
- Lua 数据类型和 Redis 数据类型之间转换
当 Lua 通过 call() 或 pcall() 函数执行 Redis 命令的时候,命令的返回值会被转换成 Lua 数据结构. 同样地,当 Lua 脚本在 Redis 内置的解释器里运行时,Lua ...
- Jedis实现发布订阅功能
Redis为我们提供了publish/subscribe(发布/订阅)功能.我们可以对某个channel(频道)进行subscribe(订阅),当有人在这个channel上publish(发布)消息时 ...
- box-sizing的不同属性值间的区别
box-sizing:值为 border-box时,其含义为:表示元素的宽度与高度包括内部补白区域(指border和padding)与边框的宽度与高度:值为content-box时,其含义正其前者相反 ...