一起刷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) + ...
随机推荐
- JBoss - 调整JVM内存 -Xms512m -Xmx1024m
$JBOSS-HOME/server/下有3个目录,all/default/minimal,它们是表示3种配置,全部的配置.默认配置.最小配置,我们在启动JBOSS服务时,可以指定 run –c al ...
- PCL—低层次视觉—点云分割(RanSaC)
点云分割 点云分割可谓点云处理的精髓,也是三维图像相对二维图像最大优势的体现.不过多插一句,自Niloy J Mitra教授的Global contrast based salient region ...
- 想要风投被你的融资 PPT 打动吗?别忘了你其实就是在想方设法卖出自己公司的部分股权
硅谷,一个常常见诸于报端,看着很熟悉,但是又不那么被人所了解的未及之地.它不是一个严格限定的地理位置,一般来说是指旧金山和湾区,其中湾区又分为东湾(East Bay)和南湾(South Bay), ...
- pt-online-schema-change
[root@mysql5 ~]# pt-online-schema-change --alter=,u=root,p=1qaz2wsx,D=test,t=ddl_test --print --dry- ...
- [ZOJ1610]Count the Colors(线段树,区间染色,单点查询)
题目链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=1610 题意:给一个长8000的绳子,向上染色.一共有n段被染色,问染 ...
- JavaScript constructor 属性
定义和用法 constructor 属性返回对创建此对象的数组函数的引用. 语法 object.constructor 实例 例子 1 在本例中,我们将展示如何使用 constructor 属性: & ...
- 函数buf_read_page_low
/********************************************************************//** Low-level function which r ...
- bzoj4011
好题,首先有一个结论,有向无环图的树形图数目=根节点意外入度之积 现在相当于在原图上加一条边问树形图的数目 考虑多出来不合法的方案,一定是成环且包含新加入的边 对于一条路贡献就是∏d[i] [i∉pa ...
- 【笨嘴拙舌WINDOWS】编码历史
在介绍历史之前,有必要将一个经常使用的词语"标准"解释一下: " 标准是"为了在一定的范围内获得最佳秩序,经协商一致制定并由公认机构批准,共同使用的和重复使用的 ...
- asp.net 使用JQuery 调用Ashx 后面直接写方法名,通过反射找到对应的方法
using System.Reflection; public class Industry_Manager : IHttpHandler { HttpRequest gRequest = null; ...