2.Add Two Numbers(List To Long)

本题题意是指将两个数倒序存储在链表中,再将两数之和同样存储在链表中输出。

我最开始的思路是将每一位相加,再考虑是否进位,但这时就需要考虑一些情况,比较麻烦。

于是我决定采取另一种在网上新学到的方法:这个方法就是将链表中的数字串起来,当做一个long,例如2->4->5,可以根据题目具体要求转化成long型的542,再做后续的操作,就很容易了。

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution
10 {
11 public:
12     long ListToLong(ListNode* l)
13     {
14         long res = 0;
15         long tem = 1;
16         while(l)
17         {
18             res = res + l->val * tem;
19             tem = tem * 10;
20             l = l->next;
21         }
22         return res;
23     }
24     long get_n(long num)
25     {
26         long x = 10;
27         long n = 1;
28         while(num >= x)
29         {
30             n ++;
31             x = x * 10;
32         }
33         return n;
34     }
35     void add(ListNode* l1, ListNode* l2)
36     {
37         while(l1->next)
38         {
39             l1 = l1->next;
40         }
41         l1->next = l2;
42     }
43     ListNode* LongToList(long num)
44     {
45         ListNode* res = new ListNode(-1);
46         int n = get_n(num);
47         //cout<<"n = "<<n<<endl;
48         int x = 0;
49         for(int i = 0;i < n;i ++)
50         {
51             x = num % 10;
52             num = num / 10;
53             ListNode* node = new ListNode(x);
54             add(res,node);
55         }
56         return res->next;
57     }
58     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
59     {
60         long num1 = ListToLong(l1);
61         long num2 = ListToLong(l2);
62         //cout<<"num1 = "<<num1<<" num2 = "<<num2<<endl;
63         long num3 = num1 + num2;
64         //cout<<"num3 = "<<num3<<endl;
65         ListNode* res = LongToList(num3);
66         return res;
67     }
68 };

但上面的代码提交后的结果很让我无语。。。见下图

只有两个示例没有通过。。。

时间: 2024-10-29 12:06:44

2.Add Two Numbers(List To Long)的相关文章

2、Add Two Numbers

1.Add Two Numbers--这是leedcode的第二题: 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:

LeetCode --- 2. Add Two Numbers

题目链接:Add Two Numbers 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

【LeetCode】Add Two Numbers

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

Add Two Numbers(Linked List)

Add Two Numbers 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) + (

Add Two Numbers

题目: Add Two Numbers 题目描述: 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. 程序设计 链表节点定义 Def

Leetcode 02 Add Two Numbers

Add Two Numbers 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) + (

【Leet Code】Add Two Numbers

Add Two Numbers Total Accepted: 20255 Total Submissions: 88115My Submissions 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 numb

LeetCode: Add Two Numbers 题解

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

leetcode速度才是王道 2. Add Two Numbers

2. Add Two Numbers 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)

leetcode 002 Add Two Numbers

Add Two Numbers My Submissions Question Total Accepted: 105768 Total Submissions: 494750 Difficulty: Medium 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