Add Two Numbers
来源:互联网 发布:亚马逊数据 测评 编辑:程序博客网 时间:2024/06/18 12:30
问题描述:
You are given two non-empty linked lists representing two non-negative integers. 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.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
代码:
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { struct ListNode* res, *L1=l1, *L2=l2, *temp; while (L1&&L2) { L1->val=L2->val=L1->val+L2->val; L1 = L1->next; L2 = L2->next; } res = (L1) ? l1 : l2; temp=res; while (temp) { if (temp->val>=10) { if (!temp->next) temp->next = (struct ListNode*)calloc(1,sizeof(struct ListNode)); temp->val-=10; temp->next->val+=1; } temp = temp->next; } return res;}
0 0
- Add two numbers & Add binary
- LeetCode: Add Two Numbers
- Add Two Numbers
- LeetCode: Add Two Numbers
- [LeetCode]Add Two Numbers
- LeetCode Add Two Numbers
- Leetcode: Add Two Numbers
- Add Two Numbers
- LeetCode : Add Two Numbers
- LeetCode Add Two Numbers
- [LeetCode] Add Two Numbers
- LeetCode4: Add Two Numbers
- Leetcode : Add Two Numbers
- Add Two Numbers
- 【leetcode】Add Two Numbers
- add two numbers
- Add Two Numbers
- Add Two Numbers
- java 单向链表的基本操作
- 同时查询多张表中的数据个数
- Struts2数据校验(2)--使用XWork检验框架实现校验
- @RequestMapping 用法详解之地址映射(转)
- 桶排序算法实现-PHP
- Add Two Numbers
- i.MX6DL uboot 中 i2c4 总线配置
- 【转载】程序员谈学习:我为什么要学习Linux?
- 冒泡排序
- 企业微信对接操作
- quick cocos2dx grid3d 特效整理
- [windows]Android Studio代码重构常用快捷键
- 理解html语义化
- Android之Activity(二)生命周期