一起刷LeetCode2-Add Two Numbers
今天看不进去论文,也学不进去新技术,于是先把题刷了,一会补别的。
-----------------------------------------------------我才不是分割线-------------------------------------------------
Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order
and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
【题意】:就是给你两个链表,链表内的内容为一个多位数的逆序,比如342就表示为(2 -> 4 -> 3),现在让你算这两个多位数的和,
同时用相同的方法表示出来,也就是将和的逆序用链表表示出来。
【心路历程】看到题目一开始的想法就是考查模拟加法+链表操作的题,还算简单,链表操作就是一个尾部加链表的方法。
于是开始码代码,写完一交发现出现RE (TAT)。错误的样例为[0],[0]。想了半天不知道哪里错了。。。
后来发现我head指针没分配空间,额额额,改完一交,AC (^ ^)
------------------------------------------------------------------------------------------------------------------------
代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode * res = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode * ans = NULL;
int judge = ;
int save = ;
int add1,add2,sum;
if(l1 == NULL && l2 == NULL) return NULL;
while(l1 != NULL || l2 != NULL) {
if(l1 == NULL) {
add1 = ;
add2 = l2->val;
l2 = l2->next;
}else if(l2 == NULL){
add1 = l1 ->val;
add2 = ;
l1 = l1->next;
}else {
add1 = l1->val;
add2 = l2->val;
l1 = l1->next;
l2 = l2->next;
}
sum = add1 + add2 + save;
save = sum / ;
struct ListNode * temp = (struct ListNode *)malloc(sizeof(struct ListNode));
temp->val = sum % ;
temp->next = NULL;
res->next = temp;
if(judge == ) {
ans = temp;
judge = ;
}
res = res->next;
}
if(save){
struct ListNode * temp = (struct ListNode *)malloc(sizeof(struct ListNode));
temp->val = save;
temp->next = NULL;
res->next = temp;
}
return ans;
}
一起刷LeetCode2-Add Two Numbers的更多相关文章
- Leetcode-2 Add Two Numbers
#2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...
- leetcode2:Add Two Numbers
Add Two Numbers Total Accepted: 55216 Total Submissions: 249950My Submissions You are given two link ...
- Leetcode2:Add Two Numbers@Python
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- leetcode-2 Add Two Numbers 计算两个对应的列表和问题
1.问题描写叙述: You are given two linked lists representing two non-negativenumbers. The digits are sto ...
- LeetCode-2. Add Two Numbers(链表实现数字相加)
1.题目描述 You are given two non-empty linked lists representing two non-negative integers. The digits a ...
- Leetcode2.Add Two Numbers两数相加
给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...
- LeetCode 2. 两数相加(Add Two Numbers)
2. 两数相加 2. Add Two Numbers 题目描述 You are given two non-empty linked lists representing two non-negati ...
- (python)leetcode刷题笔记 02 Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- 【LeetCode445】 Add Two Numbers II★★
题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...
随机推荐
- 231. Power of Two
题目: Given an integer, write a function to determine if it is a power of two. 链接: http://leetcode.com ...
- YUV到RGB的转换
以下内容来源于网络,下面三个链接里的内容是比较好的,感谢博主的分享. http://blog.csdn.net/housisong/article/details/1859084 http://blo ...
- 修改tabbarcontroller选中图片及选中颜色
1.修改选中图片: UITabBarItem* item = [self.tabBarController.tabBar.items objectAtIndex:1]; //从0开始 item.s ...
- TeeChart的X轴为时间,多个Y轴的显示
最后上代码 public partial class Test : Form { private TChart tChart = new TChart(); ; public Test() { Ini ...
- webbrowser代理c#代码实现
微软webbrowser控件也就是IE插件,他的所有功能就像IE类似,当然设置也是一样的,下面介绍下webbrowser如何设置代理,可不要用这个对抗广告联盟哦 You can change the ...
- 【Todo】抽象渗漏法则 & 找到理想员工 & 软件开发成功 12 法则 & Joel on Software
Joel应该是个软件专家,这是他文章汇总的中文版本: http://local.joelonsoftware.com/wiki/Chinese_%28Simplified%29 其中有几篇值得好好看看 ...
- Parallel并行运算实例
并行运算Parallel,是.net 4.0版本里添加的新处理方式,主要充分利用CPU.任务并发的模式来达到提高运算能力.简单理解为每个CPU都在处理任务,而不会让它们空闲下来. 直接看实例: nam ...
- Jqgrid入门-结合Struts2+json实现数据展示(五)
DEMO用的是ssh框架实现的,具体怎么搭建的就不多做说明了.分页表格的数据操作难点就是数据展现.至于增删改直接用hibernate原生的方法实现即可. 初步分析:表格要实现分页,那么 ...
- MYSQL的分区字段,必须包含在主键字段内
MYSQL的分区字段,必须包含在主键字段内 MYSQL的分区字段,必须包含在主键字段内 在对表进行分区时,如果分区字段没有包含在主键字段内,如表A的主键为ID,分区字段为createtime ,按 ...
- git - svn 平滑到 git
1. 建立自己的git仓库,需要是空git仓库 2. checkout 你的 git仓库 3. svn忽略.git文件,忽略.git .gitignore 4. 把 .git文件拷到你的 svn仓库 ...