亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

[LintCode/LeetCode] Add Two Numbers

hedzr / 438人閱讀

Problem

You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1"s digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.

Example

Given 7->1->6 + 5->9->2. That is, 617 + 295.

Return 2->1->9. That is 912.

Given 3->1->5 and 5->9->2, return 8->0->8.

Note

[null]

Solution
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if (l1 == null) return l2;
        if (l2 == null) return l1;
        ListNode dummy = new ListNode(0);
        ListNode head = dummy;
        int carry = 0;
        while (l1 != null || l2 != null) {
            int sum = carry;
            if (l1 != null) {
                sum += l1.val;
                l1 = l1.next;
            }
            if (l2 != null) {
                sum += l2.val;
                l2 = l2.next;
            }
            carry = sum/10;
            head.next = new ListNode(sum%10);
            head = head.next;
        }
        if (carry != 0) head.next = new ListNode(carry);
        return dummy.next;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/65688.html

相關(guān)文章

  • [LintCode/LeetCode] Two Sum

    摘要:就不說了,使用的解法思路如下建立,對(duì)應(yīng)該元素的值與之差,對(duì)應(yīng)該元素的。然后,循環(huán),對(duì)每個(gè)元素計(jì)算該值與之差,放入里,。如果中包含等于該元素值的值,那么說明這個(gè)元素正是中包含的對(duì)應(yīng)的差值。返回二元數(shù)組,即為兩個(gè)所求加數(shù)的序列。 Problem Given an array of integers, find two numbers such that they add up to a s...

    xiaoxiaozi 評(píng)論0 收藏0
  • [LintCode/LeetCode] Subsets & Subsets II

    Subsets Problem Given a set of distinct integers, return all possible subsets. Notice Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets. Example ...

    tracy 評(píng)論0 收藏0
  • [LintCode/LeetCode] Single Number III

    摘要:去掉最后一個(gè)保留最后一個(gè)保留最后一個(gè)保留第一個(gè)這道題在論壇里參考了和兩位仁兄的解法。思想是將中所有的數(shù)進(jìn)行異或運(yùn)算。不同的兩個(gè)數(shù)異或的結(jié)果,一定至少有一位為。最后,將和存入數(shù)組,返回。 Problem Given 2*n + 2 numbers, every numbers occurs twice except two, find them. Example Given [1,2,2...

    lanffy 評(píng)論0 收藏0
  • [LintCode/LeetCode] Combinations

    摘要:題目為求從到的自然數(shù)里取個(gè)數(shù)的所有組合全集。使用遞歸的模板,建立函數(shù)。模板如下也可以不建立新的,而是遞歸調(diào)用之后刪去中最后一個(gè)元素 Problem Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example For example,If n = 4 a...

    Raaabbit 評(píng)論0 收藏0
  • [LintCode/LeetCode] Find Median From / Data Stream

    摘要:建立兩個(gè)堆,一個(gè)堆就是本身,也就是一個(gè)最小堆另一個(gè)要寫一個(gè),使之成為一個(gè)最大堆。我們把遍歷過的數(shù)組元素對(duì)半分到兩個(gè)堆里,更大的數(shù)放在最小堆,較小的數(shù)放在最大堆。同時(shí),確保最大堆的比最小堆大,才能從最大堆的頂端返回。 Problem Numbers keep coming, return the median of numbers at every time a new number a...

    zxhaaa 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

hedzr

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<