LeetCode - 2. Add Two Numbers

简介: 2. Add Two Numbers  Problem's Link  ---------------------------------------------------------------------------- Mean:  给你两个数字链表,让你将两个链表相加,结果保存在一个新链表中.

2. Add Two Numbers 

Problem's Link

 ----------------------------------------------------------------------------

Mean: 

给你两个数字链表,让你将两个链表相加,结果保存在一个新链表中.

analyse:

最基本的链表操作.

做链表题时只需注意:先分配(new ListNode(val)),再h=h->next.也就是不要将指针指向空指针.

Time complexity: O(N)

 

view code

/**
* -----------------------------------------------------------------
* Copyright (c) 2016 crazyacking.All rights reserved.
* -----------------------------------------------------------------
*       Author: crazyacking
*       Date  : 2016-01-29-16.16
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long( LL);
typedef unsigned long long( ULL);
const double eps( 1e-8);

// 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)
    {
        int jinwei = 0;
        ListNode * h1 = l1;
        ListNode * h2 = l2;
        ListNode * ans , * ret;
        bool isFirst = true;
        while( h1 && h2)
        {

            int val = h1 -> val + h2 -> val + jinwei;
            int now = val % 10;
            jinwei = val / 10;
            if( isFirst)
            {
                ans = new ListNode( now);
                ret = ans;
                isFirst = false;
            }
            else
            {
                ans -> next = new ListNode( now);
                ans = ans -> next;
            }
            h1 = h1 -> next;
            h2 = h2 -> next;
        }
        while( h1)
        {
            int val = h1 -> val + jinwei;
            int now = val % 10;
            jinwei = val / 10;
            if( isFirst)
            {
                ans = new ListNode( now);
                ret = ans;
                isFirst = false;
            }
            else
            {
                ans -> next = new ListNode( now);
                ans = ans -> next;
            }
            h1 = h1 -> next;
        }
        while( h2)
        {
            int val = h2 -> val + jinwei;
            int now = val % 10;
            jinwei = val / 10;
            if( isFirst)
            {
                ans = new ListNode( now);
                ret = ans;
                isFirst = false;
            }
            else
            {
                ans -> next = new ListNode( now);
                ans = ans -> next;
            }
            h2 = h2 -> next;
        }
        while( jinwei)
        {
            ans -> next = new ListNode( jinwei % 10);
            jinwei /= 10;
            ans = ans -> next;
        }
        return ret;
    }
};


int main()
{
    int n1 , n2;
    while( cin >> n1 >> n2)
    {
        ListNode * h1 , * head1;
        ListNode * h2 , * head2;
        int tmp;
        for( int i = 0; i < n1; ++ i)
        {
            cin >> tmp;
            if( ! i)
            {
                h1 = new ListNode( tmp);
                head1 = h1;
            }
            else
            {
                h1 -> next = new ListNode( tmp);
                h1 = h1 -> next;
            }
        }

        for( int i = 0; i < n2; ++ i)
        {
            cin >> tmp;
            if( ! i)
            {
                h2 = new ListNode( tmp);
                head2 = h2;
            }
            else
            {
                h2 -> next = new ListNode( tmp);
                h2 = h2 -> next;
            }
        }

        Solution solution;
        ListNode * ans = solution . addTwoNumbers( head1 , head2);
        puts( "----------------------");
        while( ans)
        {
            cout << ans -> val;
            ans = ans -> next;
        }
        cout << endl;
    }
    return 0;
}

 

目录
相关文章
|
5月前
Leetcode 623. Add One Row to Tree
题目很简单,在树的第d层加一层,值为v。递归增加一层就好了。代码如下
25 0
|
7月前
|
存储 C++ Python
LeetCode刷题---Add Two Numbers(一)
LeetCode刷题---Add Two Numbers(一)
|
10月前
|
存储 算法 安全
LeetCode - #2 Add Two Numbers
我们社区从本期开始会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。 不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。
LeetCode - #2 Add Two Numbers
LeetCode 1380. 矩阵中的幸运数 Lucky Numbers in a Matrix
LeetCode 1380. 矩阵中的幸运数 Lucky Numbers in a Matrix
LeetCode 415. Add Strings
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。
65 0
LeetCode 415. Add Strings
|
存储 Python
LeetCode 315. Count of Smaller Numbers After Self
给定一个整数数组 nums,按要求返回一个新数组 counts。数组 counts 有该性质: counts[i] 的值是 nums[i] 右侧小于 nums[i] 的元素的数量。
72 0
LeetCode 315. Count of Smaller Numbers After Self
LeetCode 258. Add Digits
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。
51 0
LeetCode 258. Add Digits
LeetCode 241. Different Ways to Add Parentheses
给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果。你需要给出所有可能的组合的结果。有效的运算符号包含 +, - 以及 * 。
51 0
LeetCode 241. Different Ways to Add Parentheses
LeetCode 67. Add Binary
给定两个二进制字符串,返回它们的总和(也是二进制字符串)。 输入字符串都是非空的,只包含字符1或0。
53 0
LeetCode 67. Add Binary
|
存储
Leetcode-Medium 2. Add Two Numbers
Leetcode-Medium 2. Add Two Numbers
51 0