首页 > 其他分享 >刷题笔记(leetcode02-两数相加)

刷题笔记(leetcode02-两数相加)

时间:2022-09-20 21:23:57浏览次数:76  
标签:total ListNode cur next leetcode02 l1 刷题 两数 struct

普通的循环解法,C代码:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     struct ListNode *next;
 6  * };
 7  */
 8 
 9 
10 struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
11     int total = 0, next1 = 0;
12 
13     struct ListNode *result = (struct ListNode *)malloc(sizeof(struct ListNode));
14     struct ListNode *cur = result;
15     cur->next = NULL;
16 
17     while(l1 && l2){
18         total = l1->val + l2->val + next1;
19         cur->next = (struct ListNode *)malloc(sizeof(struct ListNode));
20         cur->next->val = total % 10;
21         cur->next->next = NULL;
22         next1 = total / 10;
23         l1 = l1->next;
24         l2 = l2->next;
25         cur = cur->next;
26     }
27 
28     while(l1){
29         total = l1->val + next1;
30         cur->next = (struct ListNode *)malloc(sizeof(struct ListNode));
31         cur->next->val = total % 10;
32         cur->next->next = NULL;
33         next1 = total / 10;
34         l1 = l1->next;
35         cur = cur->next;
36     }
37 
38     while(l2){
39         total = l2->val + next1;
40         cur->next = (struct ListNode *)malloc(sizeof(struct ListNode));
41         cur->next->val = total % 10;
42         next1 = total / 10;
43         cur->next->next = NULL;
44         l2 = l1->next;
45         cur = cur->next;
46     }
47 
48     if(next1 != 0){
49         cur->next = (struct ListNode *)malloc(sizeof(struct ListNode));
50         cur->next->val = next1;
51         cur->next->next = NULL;
52     }
53 
54     return result->next;
55 
56 }
View Code

 

标签:total,ListNode,cur,next,leetcode02,l1,刷题,两数,struct
From: https://www.cnblogs.com/yx12789/p/16712587.html

相关文章

  • 学习-数组相关算法-两数之和
    obj[1]=111letobj={"5":222}console.log(obj[5])////222console.log(obj['5'])//222//console.log(obj.5)这样写会报错obj[1]=111console.log(JSO......
  • 【刷题】【线段树】
    (1)Laz标记:题目:区区区间区区区间(nowcoder.com)题解摘自:区区区间_牛客博客(nowcoder.net)我们发现这个等差数列的等差为1。对于修改一段区间l-rl−r如果我们知道......
  • 两数之和
    var twoSum = function(nums, target) {    const map=new Map();    for(let i=0;i<nums.length;i++){        //来找对象的值      ......
  • 记刷题过程中发现的C++与C的差异
    前言上大学了,学c。标题嫖自@快乐永恒正题01#include<stdio.h>intmain(){longlonga,b;scanf("%lld%lld",&a,&b);printf("%lld%lld%lld%lld%l......
  • LeetCode 1 两数之和
    classSolution{public:vector<int>twoSum(vector<int>&nums,inttarget){unordered_map<int,int>map;for(inti=0;i<nums.size();......
  • 力扣leetcode刷题记录1----
    【以下题目来源均来自力扣leetcode】 595.大的国家World 表:【描述】name是这张表的主键。这张表的每一行提供:国家名称、所属大陆、面积、人口和GDP值。【问题】......
  • 一款好用免费的前端刷题小程序
    今天给大家安利一款免费好用的前端刷题小程序,5G面试宝典首先打开小程序,映入眼帘的是简洁大方的界面。系统分成了五大功能区,分别是首页,文章,我的错误,排行榜,以及我的。再细分......
  • 【JS每日刷题】栈与任务队列1
    代码题目来源于前端面试题宝典constfoo=()=>console.log('First')constbar=()=>setTimeout(()=>console.log('Second'))constbaz=()=>console.log('T......
  • MySQL刷题复习笔记 - 每日持续更新
    PS为了代码规范,所以所有关键字均为大写,其他为小写。点击题目名称即为题解链接。MySQL基本语法SELECT[DISTINCT]列名1,列名2...FROM表名WHERE查询条件表达......
  • 刷题篇。树、图、数组等相关常见处理操作
    一、树 序号做什么方法伪码 1前、中、后遍历    2以任意节点为根重构树    ......