leetcode 2:Add Two Numbers(C语言)
来源:互联网 发布:简易三维画图软件 编辑:程序博客网 时间:2024/06/03 16:53
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
// if(l1==NULL)return l2;
// if(l2==NULL)return l1;
int addition = 0;
int sum = 0;
struct ListNode* res = (struct ListNode*)malloc(sizeof(struct ListNode)),*p=l1,*q=l2;
res->val =0;
res->next=NULL;
struct ListNode* head = res;
while(p!=NULL || q!=NULL){
res->val +=(p!=NULL?p->val:0) + (q!=NULL?q->val:0);
if(res->val<=9)
addition=0;
else{
addition=1;res->val-=10;
}
printf("addition :%d",addition);
if( ((p!=NULL)?(p->next!=NULL):0)||((q!=NULL)?(q->next!=NULL):0)||addition==1)
{
res->next = (struct ListNode*)malloc(sizeof(struct ListNode));
res->next->val=addition ;
res->next->next=NULL;
res=res->next;
}
if(p!=NULL)p=p->next;
if(q!=NULL)q=q->next;
}
return head;
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
// if(l1==NULL)return l2;
// if(l2==NULL)return l1;
int addition = 0;
int sum = 0;
struct ListNode* res = (struct ListNode*)malloc(sizeof(struct ListNode)),*p=l1,*q=l2;
res->val =0;
res->next=NULL;
struct ListNode* head = res;
while(p!=NULL || q!=NULL){
res->val +=(p!=NULL?p->val:0) + (q!=NULL?q->val:0);
if(res->val<=9)
addition=0;
else{
addition=1;res->val-=10;
}
printf("addition :%d",addition);
if( ((p!=NULL)?(p->next!=NULL):0)||((q!=NULL)?(q->next!=NULL):0)||addition==1)
{
res->next = (struct ListNode*)malloc(sizeof(struct ListNode));
res->next->val=addition ;
res->next->next=NULL;
res=res->next;
}
if(p!=NULL)p=p->next;
if(q!=NULL)q=q->next;
}
return head;
}
*************? : 优先级问题导致一直通不过
0 0
- [C语言][LeetCode][2]Add Two Numbers
- leetcode 2:Add Two Numbers(C语言)
- 【LeetCode】2. Add Two Numbers C语言
- Leetcode c语言-Add Two Numbers
- 【leetcode-2】Add Two Numbers(C)
- [LeetCode#2][C]Add Two Numbers
- 【leetcode 2】 Add Two Numbers(C++)
- [LeetCode] Add Two Numbers(C++)
- 【LeetCode编程学习(C语言)】2.Add Two Numbers
- leetcode 2. Add Two Numbers C语言实现
- 【LeetCode】 445. Add Two Numbers II C语言
- 【LeetCode算法练习(C语言)】Add Two Numbers
- LeetCode 2 - Add Two Numbers
- LeetCode 2: Add Two Numbers
- Leetcode: Add Two Numbers (2)
- [leetcode 2] Add Two Numbers
- [Leetcode] 2 - Add Two Numbers
- LeetCode 2:《Add Two Numbers》
- Android: Bitmap与DrawAble与byte[]与InputStream之间的转换
- 【UVa】455 - Periodic Strings
- Stanford机器学习__Lecture notes CS229. Logistic Regression(逻辑回归)(2)Perceptron Learning Algorithm
- Java.lang包
- 2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D:Distribution in Metagonia(构造)
- leetcode 2:Add Two Numbers(C语言)
- HashMap源码剖析
- 2016最新版App Store应用审核指南完整版
- C++Primer(第六章课后习题)(程序题源代码)
- 在群晖 DS213j下安装ipkg,aria2
- QQ粘虫密码破解器ASP后台稳定收信免杀版
- php从入门到放弃.Day2
- 线程篇(一):Java中的线程(基础)
- 使用maven构建web项目实例