[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/HSUAN221/leetcode/main/2_Add_Two_Numbers.cpp [Back]  [Original]

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        auto dummy = new ListNode(-1);
        auto curr = dummy;
        int carry = 0;
        while (l1 != nullptr || l2 != nullptr) {
            int num1 = (l1 == nullptr ? 0 : l1->val);
            int num2 = (l2 == nullptr ? 0 : l2->val);
            // std::cout next;
        }
        if (carry != 0) {
            curr->next = new ListNode(carry);
        }
        return dummy->next;
    }
};

Web Proxy Viewer  |  New URL  |  Original Page