2-Add Two Numbers @LeetCode

题目

思路

题目中得到的信息有:

  1. 这是两个非负数,每位分别保存在链表的一个结点上;
  2. 逆序保存,从低位到高位依次。

一般整数的相加都是从低往高进行,和保存的顺序一致,因此一次遍历就可完成,可以看出这道题目不难。

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的更多相关文章

  1. Add Two Numbers LeetCode Java

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  2. Add two numbers [LeetCode]

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  3. Add Two Numbers ---- LeetCode 002

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  4. LeetCode 面试:Add Two Numbers

    1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  5. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  6. [LeetCode] Add Two Numbers 两个数字相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  7. LeetCode Add Two Numbers II

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...

  8. leetcode 第二题Add Two Numbers java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

  9. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  10. LeetCode:1. Add Two Numbers

    题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...

随机推荐

  1. Redis 分布式锁及缓存注释的使用方法

    使用工具:Apache an 测压命令: ab -n 100 -c 100 http://www.baidu.com -n代表模拟100个请求,-c代表模拟100个并发,相当于100个人同时访问 ab ...

  2. NFS、FTP介绍

    第二十五课 NFS.FTP介绍 目录 一. NFS介绍 二.NFS服务端安装配置 三.NFS配置选项 四.exportfs命令 五.NFS客户端问题 六.FTP介绍 七.使用vsftpd搭建ftp 八 ...

  3. php字符串转数组

    下面代码包括了含有中文汉字的字符. function str2arr($str) { $length = mb_strlen($str, 'utf-8'); $array = []; for ($i= ...

  4. SpringCloud----熔断机制 -- 断路器hystrix

    参考借鉴:http://www.cnblogs.com/chry/p/7279856.html SpringCloud Netflix实现了断路器库的名字叫Hystrix. 在微服务架构下,通常会有多 ...

  5. 前端特效demo | 值得收藏的6个 HTML5 Canvas 实用案例

    HTML5 动画在Canvas 上得到了充分的发挥,我们 VIP 视频也分享过很多相关的动画特效制作视频,这次给大家带来 6 款超炫酷的HTML5 canvas 动画的 demo,一起来看看吧~ 文内 ...

  6. elasticsearch的监控脚本

    监控elasticsearch cluster 集群 通过主动模式将数据发送给zabbix server import json import struct import socket import ...

  7. 喝汤 beautifulsoup 批量爬取图片

    未成功 from urllib.request import urlopen import re import random base_url = "http://www.meizitu.c ...

  8. ASP.NET Web API之消息拦截

    要在action执行前后做额外处理,那么ActionFilterAttribute和ApiControllerActionInvoker就派上用场了.比如客户端请求发过来的参数为用户令牌字符串toke ...

  9. web移动端类型检测

    移动端检测 插件通用下载: https://www.bootcdn.cn/ 根据一个库 device.js 下载地址 传送 api 传送 和 传送 常用检测类型 device.ipad() 返回一个布 ...

  10. git教程:管理修改

    转载:管理修改 现在,假定你已经完全掌握了暂存区的概念.下面,我们要讨论的就是,为什么Git比其他版本控制系统设计得优秀,因为Git跟踪并管理的是修改,而非文件. 你会问,什么是修改?比如你新增了一行 ...