LeetCode:Add Two Numbers

这是第一次用markdown写东西啊,本来写的就少,试一试吧。


题目:
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


题目的大意是给出两个链表,每个链表代表的是一个整数,不过每一位都存储在一个Node里,现在给你两个链表,让两个链表相加,并返回链表的头,我能想到的有两种思路:

  • 先求出两个链表里面的值,然后相加,把最后的得到的值的每一位在去重新创建一个新的链表,返回链表头,由于链表里面的值可能会很大,这个用c++写,可能不太好用啊。大数相加没有java方便,这个没写。后来想想这个思路是行的,但估计不能通过,写了一下,没法识别BigInteger这个类。

  • 直接相加,进位处理。

#include <iostream>
#include <stdlib.h>   // 引入malloc的头文件
using namespace std;
/**
*  Add Two Numbers Total Accepted: 55484 Total Submissions: 251336 My Submissions Question Solution
*  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
*/
/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

// 用一个数组去创建一个链表,并返回链表头指针
ListNode * CreateLinkList(int a[], int n)
{
    ListNode * head = (ListNode *)malloc(sizeof(ListNode));
    ListNode * beginhead = head;

    for (int i = 0; i<n; i++)
    {
        ListNode * node = (ListNode *)malloc(sizeof(ListNode));
        node->val = a[i];
        head->next = node;
        head = node;
    }
    head->next = NULL;
    return beginhead->next;
}

/**
* 打印链表内容
*
*/
void Print(ListNode * head)
{
    while (head != NULL)
    {
        cout << head->val << " ";
        head = head->next;
    }
}
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
    {
        int i = 1;
        int m = 0;
        ListNode *beginhead;
        ListNode * head = (ListNode*)malloc(sizeof(ListNode));
        beginhead = head;
        while (l1 != NULL && l2 != NULL)
        {
            int sum = (l1->val + l2->val);
            int n = sum%10;
            if (m == 1)
                n = n + 1;
            m = sum / 10;
            ListNode * node = (ListNode *)malloc(sizeof(ListNode));
            node->next = NULL;
            node->val = n;
            if (n >= 10)
            { // {3,7} {9,2}
                node->val = n % 10;
                m = 1;
            }
            head->next = node;
            head = node;

            l1 = l1->next;
            l2 = l2->next;
        }

        if (l1!=NULL)
        {       //{9,9} {1}
            while (l1 != NULL)
            {
                int l1Val = l1->val;
                if (m == 1)
                    l1Val = l1->val+1;
                m = l1Val / 10;
                l1->val = l1Val % 10;  // 取的是余数
                head->next = l1;
                head = head->next;
                l1 = l1->next;
            }

            if (m == 1)
            {
                ListNode * node = (ListNode*)malloc(sizeof(ListNode));
                node->next = NULL;
                head->next = node;
                node->val = 1;
            }
            return beginhead->next;
        }

        if (l2 != NULL)
        {//{1}{9,9}
            while (l2 != NULL)
            {
                int l2Val = l2->val;
                if (m == 1)
                    l2Val =l2->val+1;
                m = l2Val / 10;
                l2->val = l2Val % 10;  // 取的是余数
                head->next = l2;
                head = head->next;
                l2 = l2->next;
            }

            if (m == 1)
            {
                ListNode * node = (ListNode*)malloc(sizeof(ListNode));
                node->next = NULL;
                head->next = node;
                node->val = 1;
            }
            return beginhead->next;

        }
        if (m == 1)
        {
            ListNode * node = (ListNode *)malloc(sizeof(ListNode));
            node->next = NULL;
            head->next = node;
            head = node;
            head->val = 1;

        }
        return beginhead->next;

    };
};

int main()
{
/**
 *   {1} {9,9}
 *   {9,9} {1}
 *   {0} {0}
 *   {3,7} {9,2}
 */
    int b[] = {3,7};
    int a[] = {9,2};

    ListNode *l1 = CreateLinkList(a, sizeof(a) / sizeof(int));
    ListNode *l2 = CreateLinkList(b, sizeof(b) / sizeof(int));
    Solution *s = new Solution;
    Print(s->addTwoNumbers(l1, l2));
    system("pause");
    return 0;
}

这个程序大概写了3、4个小时,慢慢来吧,先说说遇到的问题或者说要熟悉的吧,虽然慢慢写能完成,但是很不熟悉。
1. 创建链表的时候,没有给最后一个元素赋NULL,这样就是一个死循环了,malloc这个头文件包含在stdlib.h这个头文件里面。
2. 感觉好多测试用例通不过,但是leetcode都给出了测试例子,感觉这个还是比较好的,虽然错了,但知道错哪里了,可以慢慢的去调试。
3. malloc一个新的node的时候最好这个node->next全都赋NULL.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值