[2, Medium, C++] Add Two Numbers

本文介绍了一种利用链表数据结构实现两个非负整数相加的方法。通过将数字以逆序方式存储在链表节点中,并逐位进行相加,最终形成一个新的链表来表示相加结果。文章详细解释了处理进位、更新链表尾部等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem:

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 -> 4)
Output: 7 -> 0 -> 8

Subscribe to see which companies asked this question

Analysis:

This problem shows the some fundamental operations of linked-lists. We list them as follows:

1. For most of cases, I prefer to define a new head so that it is easy to return the value. Then, the basic procedure to solve similar questions is to move the updated node of some original list to the newly created list.

2. When to add new nodes to a newly list, we need two pointers: one for the new list head and the other one is to indicate the tail of the list. When the two pointers are same (they are all NULL), it means that the new list is empty. At this time, we should update the two pointers to the same node. Otherwise, we should first update the next pointer of the tail node and slip it to its next node.

3. Usually, we always set the next pointer of the tail node to be NULL for the sake of safety.

4. For this problem, the addition may need to create a new node next to the tail node of the new list.

Sample code:

    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        if(l1 == NULL)
            return l2;
        if(l2 == NULL)
            return l1;
            
        ListNode *p_sum = NULL;
        ListNode *p_cur = NULL;
        bool overflow = false;
        while(l1 != NULL && l2 != NULL) {
            l1->val += l2->val + (overflow ? 1 : 0);
            if(p_cur == NULL) {
                p_sum = l1;
                p_cur = l1;
            } else {
                p_cur->next = l1;
                p_cur = p_cur->next;
            }
            l1 = l1->next;
            l2 = l2->next;
            p_cur->next = NULL;
            overflow = (p_cur->val >= 10);
            p_cur->val %= 10;
        }
        
        ListNode *p_unfinished = (l1 == NULL ? l2 : l1);
        
        if(p_unfinished == NULL && overflow) {
            p_cur->next = new ListNode(1);
        } else if(p_unfinished) {
            while(p_unfinished) {
                if(!overflow) {
                    p_cur->next = p_unfinished;
                    break;
                }
                p_unfinished->val += (overflow ? 1 : 0);
                overflow = (p_unfinished->val >= 10);
                p_unfinished->val %= 10;
                p_cur->next = p_unfinished;
                p_unfinished = p_unfinished->next;
                p_cur = p_cur->next;
                p_cur->next;
            }
            
            if(overflow)
                p_cur->next = new ListNode(1);
        }
        
        return p_sum;
    }


要在C语言中添加日志功能,可以使用log4c或log4cplus这两个第三方库来实现。log4c是一个用于C语言的日志库,而log4cplus是一个用于C++的日志库。这两个库都有详细的文档和示例代码可供参考。 如果你使用的是Win10和VS2017开发环境,你可以按照以下步骤来使用log4c或log4cplus: 1. 首先,下载并安装log4c或log4cplus。你可以在它们的官方网站或其他开源软件站点上找到它们的下载链接。 2. 在你的项目中添加log4c或log4cplus的头文件和库文件。具体的步骤可以参考它们的官方文档。 3. 在你的代码中引入log4c或log4cplus的头文件,并按照它们的文档来配置和初始化日志系统。 4. 在你的代码中使用日志功能,例如记录日志信息、设置日志级别等。你可以根据需要自定义日志输出的格式和目标。 另外,如果你想学习更多关于CMake的知识,可以参考一些开源项目如KDE、VTK、OpenCV、Caffe等的构建过程。这些项目都使用CMake作为构建工具,并且CMake的使用越来越流行。 以上是关于在C语言中添加日志功能的一些建议,请参考具体的文档和示例代码来详细了解和实践。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [【小沐学C++C++实现日志功能:log4c(Win10+VS2017+CMake)](https://blog.youkuaiyun.com/hhy321/article/details/122787300)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值