标签:val add http ret amp int ext BMI 两数相加
地址:https://leetcode-cn.com/problems/add-two-numbers/submissions/
大意:给定两个链表,返回一个由这两个链表相加而得到的新链表
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *head = new ListNode(0);
ListNode *hh = head;
int i = 0;
while(true){
if(l1 == NULL && l2 == NULL && i == 0){
return hh->next;
}
head->next = new ListNode(0);
head = head->next;
head->val = adds(l1,l2,&i);
if(l1)
l1 = l1->next;
if(l2)
l2 = l2->next;
}
}
int adds(ListNode *l1,ListNode *l2,int *p){
int a = l1==NULL ? 0 : l1->val;
int b = l2==NULL ? 0 : l2->val;
int c = *p+a+b;
*p = 0;
if(c / 10 >= 1)
*p = c/10;
return c%10;
}
};
标签:val add http ret amp int ext BMI 两数相加
原文地址:https://www.cnblogs.com/c21w/p/12683508.html