leetcode--2. Add Two Numbers

来源:互联网 发布:m1头盔淘宝 编辑:程序博客网 时间:2024/06/04 01:39

题目:https://leetcode.com/problems/add-two-numbers/description/

代码:

class Solution {public:    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {        ListNode head(0), *res = &head;        int c=0;        while(l1||l2||c){            int temp = (l1?l1->val:0) + (l2?l2->val:0)+c;            res->next=new ListNode(temp%10);            c=temp/10;            res=res->next;            l1=l1?l1->next:l1;            l2=l2?l2->next:l2;        }        return head.next;    }};

原创粉丝点击