LeetCode "445. Add Two Numbers II"

A natural stack based solution. Seriously, whey ‘Medium‘?

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        stack<ListNode*> s1, s2;
        ListNode *p = l1;
        while(l1)
        {
            s1.push(l1); l1 = l1->next;
        }
        p = l2;
        while(l2)
        {
            s2.push(l2); l2 = l2->next;
        }

        //
        ListNode *pc = nullptr, *pp = nullptr;
        int carry = 0;
        while(!s1.empty() || !s2.empty())
        {
            int a1 = 0;
            if(!s1.empty())
            {
                a1 = s1.top()->val;
                s1.pop();
            }

            int a2 = 0;
            if(!s2.empty())
            {
                a2 = s2.top()->val;
                s2.pop();
            }

            int sum = (a1 + a2 + carry) % 10;
            carry = (a1 + a2 + carry) / 10;

            pp = new ListNode(sum);
            pp -> next = pc;
            pc = pp;
        }
        if(carry)
        {
            pp = new ListNode(carry);
            pp -> next = pc;
        }

        return pp;
    }
};

时间: 2024-10-09 20:21:35

LeetCode "445. Add Two Numbers II"的相关文章

Leetcode刷题总结: 445. Add Two Numbers II

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers

【Leetcode】445. Add Two Numbers II

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers

445 Add Two Numbers II

You are given two linked lists representing two non-negative numbers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not con

445. Add Two Numbers II ——while s1 or s2 or carry 题目再简单也要些测试用例

You are given two linked lists representing two non-negative numbers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not con

445. Add Two Numbers II 两个数字相加2

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers

445. Add Two Numbers II - Medium

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers

LeetCode 445. 两数相加 II(Add Two Numbers II)

445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 进阶: 如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转. LeetCode445. Add Two Numbers II中等 示例: 输入: (7 -> 2 -> 4 -> 3) + (5 ->

LeetCode --- 2. Add Two Numbers

题目链接:Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->