资讯专栏INFORMATION COLUMN

【LeetCode 刷題輯錄】2. Add Two Numbers

Flink_China / 2824人阅读

摘要:問題描述先來看看原題描述為方便,使用作爲實現之程式語言。初始代碼如下觀察到如下幾點題目定義了加法順序,從左至右帶進位算使用預定義的單鏈表數據結構結果返回一個類型,應該是鏈表頭。思考過程思路如圖所示實現實現

問題描述

先來看看原題描述:

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

為方便,使用 Java 作爲實現之程式語言。

初始代碼如下:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        // Your implementations here
    }
}

觀察到如下幾點:

題目定義了加法順序,從左至右帶進位運算;

使用預定義的單鏈表數據結構;

結果返回一個 ListNode 類型,應該是鏈表頭。

思考過程

思路如圖所示:

Iterative 實現
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        /* Implementation begins */
        int carry = 0;
        int sum = 0;
        ListNode result = new ListNode(0);  // keep node list
        ListNode current = result;          // current pointer
        do {
            sum = (l1 != null ? l1.val : 0) + (l2 != null ? l2.val : 0) + carry;
            current.next = new ListNode(sum % 10);
            carry = sum / 10;
            l1 = l1 != null ? l1.next : null;
            l2 = l2 != null ? l2.next : null;
            current = current.next;
        } while (l1 != null || l2 != null);
        if (carry != 0) current.next = new ListNode(carry);
        return result.next;
        /* Implementation ends */
    }
}
Recursive 實現
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(0);
        return addTwoNumbers(l1, l2, 0, dummy, dummy);
    }
    
    private ListNode addTwoNumbers(ListNode l1, ListNode l2, int carry, ListNode current, ListNode result) {
        if (l1 == null && l2 == null) {
            if (carry != 0) current.next = new ListNode(carry);
            return result.next;
        } else {
            l1 = (l1 == null) ? new ListNode(0) : l1;
            l2 = (l2 == null) ? new ListNode(0) : l2;
        }
        int sum = l1.val + l2.val + carry;
        carry = sum / 10;
        current.next = new ListNode(sum % 10);
        return addTwoNumbers(l1.next, l2.next, carry, current.next, result);
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66093.html

相关文章

  • Leetcode 2 Add Two Numbers 两数相加

    摘要:这题是说给出两个链表每个链表代表一个多位整数个位在前比如代表着求这两个链表代表的整数之和同样以倒序的链表表示难度这个题目就是模拟人手算加法的过程需要记录进位每次把对应位置两个节点如果一个走到头了就只算其中一个的值加上进位值 Add Two Numbers You are given two linked lists representing two non-negative num...

    Charlie_Jade 评论0 收藏0
  • 每日一则 LeetCode: Add Two Numbers

    摘要:描述中文解释给定两个非空的链表里面分别包含不等数量的正整数,每一个节点都包含一个正整数,肯能是,但是不会是这种情况。我们需要按照倒序计算他们的和然后再次倒序输出。 描述 You are given two non-empty linked lists representing two non-negative integers. The digits are stored in rev...

    hightopo 评论0 收藏0
  • leetcode 2 Add Two Numbers

    摘要:我们的目的是求出两个数字的加和,并以同样的形式返回。假设每个都不会存在在首位的,除非数字本身就是想法这道题主要要求还是熟悉的操作。这道题由于数字反序,所以实际上从首位开始相加正好符合我们笔算的时候的顺序。 题目详情 You are given two non-empty linked lists representing two non-negative integers. The d...

    Integ 评论0 收藏0
  • leetcode445. Add Two Numbers II

    摘要:题目要求对以链表形式的两个整数进行累加计算。思路一链表转置链表形式跟非链表形式的最大区别在于我们无法根据下标来访问对应下标的元素。因此这里通过先将链表转置,再从左往右对每一位求和来进行累加。通过栈可以实现先进后出,即读取顺序的转置。 题目要求 You are given two non-empty linked lists representing two non-negative i...

    DoINsiSt 评论0 收藏0
  • [LeetCode] 445. Add Two Numbers II

    Problem 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 ...

    alexnevsky 评论0 收藏0

发表评论

0条评论

Flink_China

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<