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
总结:指针操作还是要多练习。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
if(l2==NULL) return l1;
if(l1==NULL) return l2;
ListNode *l3=NULL;
ListNode *p = l3;
int sum;
int flag=0;
while( l1 || l2 ){
sum = flag;
if(l1){
sum += l1->val;
l1 = l1->next;
}
if(l2){
sum += l2->val;
l2 = l2->next;
}
flag = sum /10;
sum = sum%10;
if(!l3){
l3 = new ListNode(sum);
p = l3;
}else{
p->next = new ListNode(sum);
p = p->next;
}
}
if ( flag != 0){
p->next = new ListNode(flag);
}
return l3;
}
};
Climbing Stairs
Total Accepted:12531Total
Submissions:38495My Submissions
You are climbing a stair case. It takesnsteps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
class Solution {
public:
int climbStairs(int n) {
int arr[200];
int i;
arr[0] = 1;
arr[1] = 2;
for(i=2;i<n;i++){
arr[i] = arr[i-1] + arr[i-2];
}
return arr[n-1];
}
};
分享到:
相关推荐
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101_源码.zip
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
You are given two non-empty linked lists ... Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. java AC版本
leetcodebook_leetcode 1~400知识点&题型总结&leetcode对应题表
给你两个?非空 的链表,表示两个非负的整数。它们每位数字都是按照?逆序?的方式存储的,并且每个节点只能存储?一位?数字。请你将两个数相加,并以相同形式返回一个表示和的链表。你可以假设除了数字 0 之外,这两个...
leetcode高频面试笔试题150+道,亲身总结。
Leetcode 题目解答 python版本
leetcode代码、动态规划、分治算法、贪心算法
资源LeetCode__动态规划实用知识库分享知识分享
leetcode_0001_two_sum.c leetcode_0011_max_area.c leetcode_0015_three_sum.c leetcode_0016_three_sum_closest.c leetcode_0018_four_sum.c : not ready leetcode_0026_remove_duplicates.c 27, 80, 283 ...
2021-LeetCode-02_Add_Two_Numbers 这是著名的反向链表问题的“相加两个数”的简单解决方案。 如果您愿意,请随时从我的 Github 存储库链接下载并修改 XCode Playground 文件 我会定期不断地解决尽可能多的问题,以...
Leetcode_solutions_v1.html
找工作刷题,这是个很好的选择。每天刷上一个两个的题,不错。
判断字符串是否为回文数,leetcode原题,使用多种方法
leetcode怎么配置环境变量Leetcode 评分监控项目建立 步骤 0 - 项目环境设置 设置 Python 环境并获取它 mkdir ~/python-virtual-environments cd ~/python-virtual-environments python3 -m venv env source ~/...
答案LeetCode_1_TwoSum LeetCode 问题:给定一个整数数组,找出两个数字,使它们相加为特定的目标数字。 函数 twoSum 应该返回两个数字的索引,使它们相加为目标,其中 index1 必须小于 index2。 请注意,您返回的...
leetcode_practice
LeetCode_book.rar
leetcode题解 算法和数据结构题目及解答