2-Add Two Numbers @LeetCode
2-Add Two Numbers @LeetCode
题目
思路
题目中得到的信息有:
- 这是两个非负数,每位分别保存在链表的一个结点上;
- 逆序保存,从低位到高位依次。
一般整数的相加都是从低往高进行,和保存的顺序一致,因此一次遍历就可完成,可以看出这道题目不难。
C算法
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode *ret, *p, *prev;
p = ret = prev = NULL;
int flag = 0;
//先是两个整数相加
while((l1 != NULL) && (l2 != NULL) ) {
p = (struct ListNode *)malloc(sizeof(struct ListNode));
p->val = l1->val + l2->val + flag;
flag = p->val / 10;
p->val -= (flag > 0 ? 10 : 0);
if (prev != NULL) prev->next = p;
prev = p;
if (ret == NULL) ret = p;
l1 = l1->next;
l2 = l2->next;
}
//若是l1还有结点,添加上去
while(l1 != NULL) {
p = (struct ListNode *)malloc(sizeof(struct ListNode));
p->val = l1->val + flag;
flag = p->val / 10;
p->val -= (flag > 0 ? 10 : 0);
l1 = l1->next;
if (prev != NULL) prev->next = p;
if (ret == NULL) ret = p;
prev->next = p;
prev = p;
}
//若是l2还有结点,添加上去
while(l2 != NULL) {
p = (struct ListNode *)malloc(sizeof(struct ListNode));
p->val = l2->val + flag;
flag = p->val / 10;
p->val -= (flag > 0 ? 10 : 0);
l2 = l2->next;
if (prev != NULL) prev->next = p;
if (ret == NULL) ret = p;
prev->next = p;
prev = p;
}
//最后可能会有进位,要考虑到
if (flag != 0) {
p = (struct ListNode *)malloc(sizeof(struct ListNode));
p->val = flag;
prev->next = p;
prev = p;
flag = 0;
}
prev->next = NULL;
return ret;
}
结果
2-Add Two Numbers @LeetCode的更多相关文章
- Add Two Numbers LeetCode Java
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- Add two numbers [LeetCode]
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- Add Two Numbers ---- LeetCode 002
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode 面试:Add Two Numbers
1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- LeetCode:1. Add Two Numbers
题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...
随机推荐
- 第一个HTML文档
属性 和 值 1.作用 用来修饰元素 ex:让 p 标记的文本水平居中对齐 <p>Hello World</p> 2.语法 1.属性的声明必须位于开始标记里 ...
- shell练习题3
需求如下: 请按照这样的日期格式(xxxx-xx-xx)每天生成一个文件,例如今天生成的文件为2018-10-19.log, 并把磁盘的使用情况入到这个文件,(不需要写cron,写脚本即可) 参考解答 ...
- Type curtilage home
This year's National Day coincides with the Mid-Autumn festival, the double false merger about eight ...
- selenium的其他操作
# author=zyqfrom selenium import webdriverimport timedriver=webdriver.Chrome()driver.get('http://ui. ...
- vue生命周期和钩子函数
new Vue 创建vue实例 init events & liftcycle 开始初始化 beforeCreate 组件刚被创建,组件属性计算之前,如data属性等 init injecti ...
- json的xpath:简易数据查询
class JsonQuery(object): def __init__(self, data): super(JsonQuery, self).__init__() self.data = dat ...
- [c++]大数运算---利用C++ string实现任意长度正小数、整数之间的加减法
本文为大大维原创,最早于博客园发表,转载请注明出处!!! 一.概述 C/C++中的int类型能表示的范围是-2E31-2E31–1.unsigned类型能表示的范围是0-2E32–1,即 0-4294 ...
- Centos7.4 防火墙配置
# service firewalld status; #查看防火墙状态 (disabled 表明 已经禁止开启启动 enable 表示开机自启,inactive 表示防火墙关闭状态 activate ...
- python中的 uuid 模块使用示例
此模块提供不可变的 UUID 对象 (类 uuid) 和函数uuid1().uuid3().uuid4().uuid5(), 用于生成在 RFC 4122 中指定版本1.3.4和5UUIDs .如果你 ...
- vue-resource+element upload上传(遇到formData总是变为object格式)
文件上传这种业务需求很常见,但是最近用了element,仔细看了文档,按照demo写了上传,与后台传参调取接口时,控制台总是显示未获取到文件,想了又想,发现一开始思路就跑遍了... 写此博记录下遇到的 ...