题解 | #牛群的能量值#
牛群的能量值
https://www.nowcoder.com/practice/fc49a20f47ac431981ef17aee6bd7d15
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param l1 ListNode类
* @param l2 ListNode类
* @return ListNode类
*/
ListNode* addEnergyValues(ListNode* l1, ListNode* l2) {
// write code here
int carry = 0;
auto phead = new ListNode(-1);
phead ->next = nullptr;
auto cur = phead;
while(l1 && l2){
int temp = l1->val + l2->val + carry;
carry = temp / 10;
cur ->next = new ListNode(temp%10);
cur = cur->next;
l1 = l1->next;
l2 = l2->next;
}
while(l1){
int temp = l1->val + carry;
carry = temp / 10;
cur ->next = new ListNode(temp%10);
cur = cur ->next;
l1 = l1->next;
}
while(l2){
int temp = l2->val + carry;
carry = temp / 10;
cur ->next = new ListNode(temp%10);
cur = cur ->next;
l2=l2->next;
}
if(carry!=0)
cur ->next = new ListNode(carry);
return phead->next;
}
};


查看25道真题和解析