博客
关于我
LeetCode Add Two Numbers
阅读量:794 次
发布时间:2023-01-31

本文共 2539 字,大约阅读时间需要 8 分钟。

singly-linked-list: 模拟数字加法

在现实的编程练习中,我们有时会遇到需要通过模拟手动加法来完成算法实现的情况。这种方法通常用于处理类似数字相加的问题,尤其是在编写测试用例或练习数据操作时特别实用。以下,我将详细介绍如何使用singly-linked list(单向链表)的方式来模拟数字的加法,并处理可能的进位问题。


模拟数字加法的逻辑

输入

我们的输入是两个由数字节点组成的链表,每个节点包含一个数字值,还有一个指向下一个节点的指针。需要注意的是,这些节点的数字是按逆序存储的。例如,数字42被表示为2 -> 4,也就是4节点指向2节点。

制作链表

比如,输入:

(2 -> 4 -> 3) + (5 -> 6 -> 4)

我们可以创建以下链表结构:

  • 链表A:3 -> 2 -> 4
  • 链表B:4 -> 6 -> 5

加法过程

我们可以从链表的第一个节点(末尾)开始加起来,每一步计算当前节点的值之和,再处理可能的进位。具体步骤如下:

  • 初始状态:假设两个链表都有节点,我们从两者当前节点的值开始相加。
  • 创建新的节点:将相加的结果存储到新的节点中。
  • 处理进位:如果结果大于等于10,将进位值设为结果整除10的商,并将结果取余数存储到新的节点中。
  • 移动指针:将当前链表的指针向前移动一位,同时注意处理可能提前结尾的情况。
  • 完整的加法逻辑

    class Solution {public:    struct ListNode {        int val;        ListNode *next;        ListNode(int x) : val(x), next(NULL) {}    };    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {        if (l1 == NULL && l2 == NULL) return NULL;        ListNode *head = NULL;        ListNode *pre = NULL;        int carry = 0;        while (l1 && l2) {            int sum = l1->val + l2->val + carry;            carry = sum / 10;            int val = sum % 10;            ListNode *node = new ListNode(val);            if (head == NULL) {                head = node;            } else {                pre->next = node;            }            pre = node;            l1 = l1->next;            l2 = l2->next;        }        // 处理可能剩下的节点        while (l1) {            int sum = l1->val + carry;            carry = sum / 10;            int val = sum % 10;            ListNode *node = new ListNode(val);            if (head == NULL) {                head = node;            } else {                pre->next = node;            }            pre = node;            l1 = l1->next;        }        while (l2) {            int sum = l2->val + carry;            carry = sum / 10;            int val = sum % 10;            ListNode *node = new ListNode(val);            if (head == NULL) {                head = node;            } else {                pre->next = node;            }            pre = node;            l2 = l2->next;        }        // 处理最后的进位        if (carry > 0) {            ListNode *node = new ListNode(carry);            if (head == NULL) {                head = node;            } else {                pre->next = node;            }        }        return head;    }}

    总结

    通过上述算法,我们完整地完成了两个单向链表的加法操作,包括处理进位等问题。这种方法能够确保我们能正确地将两个数字相加,并以链表的形式返回结果。

    操作流程总结

  • 初始化:检查两个链表的头节点,如果都为空则返回空链表。
  • 循环加法:从两个链表的尾部节点开始逐位相加,直到至少有一个链表的节点耗尽。
  • 创建新节点:将当前相加值存储到新的链表节点中,并更新进位值。
  • 处理剩余节点:在当前链表有节点剩余时,继续加法并加上可能的进位。
  • 处理最终进位:如果最后一次加法产生了进位,将其存储到新的节点中。
  • 这种方法确保了我们能够正确地将两个数字相加,并返回合并后的结果。

    转载地址:http://jogyk.baihongyu.com/

    你可能感兴趣的文章
    Leedcode3- Max Points on a Line 共线点个数
    查看>>
    Leedcode4-sort listnode 归并排序
    查看>>
    Leedcode6-binary-tree-preorder-traversal
    查看>>
    Leedcode7-binary-tree-postorder-traversal
    查看>>
    Leetcode - Permutations I,II
    查看>>
    LeetCode 64. 最小路径和(Minimum Path Sum) 20
    查看>>
    LeetCode AutoX 安途智行专场竞赛题解
    查看>>
    LeetCode Lowest Common Ancestor of a Binary Tree
    查看>>
    LeetCode OJ:Merge k Sorted Lists(归并k个链表)
    查看>>
    leetcode Plus One
    查看>>
    LeetCode shell 题解(全)
    查看>>
    LeetCode Text Justification
    查看>>
    leetcode Valid Parentheses
    查看>>
    Leetcode | Simplify Path
    查看>>
    LeetCode – Refresh – 4sum
    查看>>
    LeetCode – Refresh – Valid Number
    查看>>
    leetcode — edit-distance
    查看>>
    LeetCode 中级 - 有序链表转换二叉搜索树(109)
    查看>>
    leetCode 字符串反转
    查看>>
    LeetCode 无重复字符的最长子串 获取字符串中不重复的子串最大长度
    查看>>