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. HashMap 的put方法

    final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { Node<K,V>[] ta ...

  2. wifi探针的使用说明.

    我使用的是四博智联提供的WIFI探针 DT-06产品 点击打开链接 1. 数据读取 可以直接通过串口即可读取数据,串口波特率设置为 115200,其它选项默认. 如果需要PC机测试,请使用杜邦线转接到 ...

  3. JavaWeb开发流程

  4. php上传文件配置

    根据需要调整php.ini文件内容,完成后重启服务器即可. 上传文件相关配置内容: file_uploads = on ;是否允许通过HTTP上传文件的开关.默认为ON即是开 upload_tmp_d ...

  5. vue简单的日历

    <ul class="date"> <li v-for="(item, index) in list" :key="index&qu ...

  6. div css 图片和文字上下居中对齐

    想要图片和文字水平居中对齐很容易,可今天做这个树的时候,遇到了要将图片上下居中.这下就不知道怎么搞. 拿手册看也看不出所以然来,不同的浏览器对DIV的支持不一个,目前最好的浏览器是Firefox. 这 ...

  7. RF新手常见问题总结

    RF新手常见问题总结--(基础篇) 1. 经常有人问这个元素找不到,一般先排除这两个地方,再自己找找A:是否等待了足够的时间让元素加载 (增加sleep xx, wait Until xxx)B:  ...

  8. 2019-04-28-day042-HTML初识

    GET / HTTP/1.1 Host: 127.0.0.1:9001 Upgrade-Insecure-Requests: 1 Accept: text/html,application/xhtml ...

  9. PHP 解决ueditor兼容问题

    默认情况下,ueditor的内容通过PHP导入到DB中,会将内容中的html进行转义操作,此时读取显示就会有问题 step 1: $content = htmlspecialchars_decode( ...

  10. 消息 xxx,级别 16,状态 x,过程 sp_executesql,第 x 行 过程需要类型为 'ntext/nchar/nvarchar' 的参数 '@statement'。

    消息 xxx,级别 16,状态 x,过程 sp_executesql,第 x 行 过程需要类型为 'ntext/nchar/nvarchar' 的参数 '@statement'. 原来的语句: ) s ...