【LeetCode】002 Add Two Numbers

题目:LeetCode 002 Add Two Numbers

题意:给定表达非负数的两个链表,这些数字按照反向顺序存储,每个节点包含一个单独的数字,将这两个数相加,返回一个新链表。

样例:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7
-> 0 -> 8

链表每个节点的结构:

1 struct ListNode {
2     int val;
3     ListNode *next;
4     ListNode(int x) : val(x), next(NULL) {}
5 };

思路:

按位相加,要考虑进位。

学习生成链表的方法:

1 ListNode *ans = new ListNode(0), *ptr = ans;
2 t->next = new ListNode(val);
3 t = t->next;
4 return ans->next;

代码1:利用数组

因为对链表特别生疏,所以尝试先将链表转成数组,然后得到结果之后再重新赋值到链表中。 但是生成链表还是参考了别人的代码:链表第一个节点只是用来传递指针的。

 1 class Solution {
 2 public:
 3     ListNode* addTwoNumbers(ListNode *l1, ListNode *l2) {
 4         int a[20000], b[20000], na = 0, nb = 0;
 5         memset(a, 0, sizeof(a));
 6         memset(b, 0, sizeof(b));
 7         while(l1)
 8         {
 9             a[na++] = l1->val;
10             l1 = l1->next;
11         }
12         while(l2)
13         {
14             b[nb++] = l2->val;
15             l2 = l2->next;
16         }
17         na = max(na, nb);
18         for(int i = 0; i < na || i < nb; i++)
19         {
20             a[i] += b[i];
21             a[i+1] += a[i]/10;
22             a[i] %= 10;
23         }
24         if(a[na]) na++;
25
26         ListNode *ans = new ListNode(0);
27         l1 = ans;
28         for(int i = 0; i < na; i++)
29         {
30             l1->next = new ListNode(a[i]);
31             l1 = l1->next;
32         }
33         return ans->next;
34     }
35 };

用链表直接做,要考虑到,如果两个链表不一样长,就会出现可能访问到链表某个节点的val为空的情况,所以要分情况进行处理。 另外,在别人的代码中看到有人说,如果其中一个链表短,访问到尾部的时候可以将另外长的链表接在返回的链表结尾,但是有个反例:

Input: (0 -> 1) + (9
-> 9 -> 9 –> 9) 或者有更多个9,需要连续进位的情况。

 1 class Solution {
 2 public:
 3     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
 4         int flag = 0;
 5         ListNode* ans = new ListNode(0);
 6         ListNode* t = ans;
 7
 8         while(l1 != NULL || l2 != NULL)
 9         {
10             int val;
11             if(!l1)
12             {
13                 if(flag == 0)
14                 {
15                     t->next = l2;
16                     break;
17                 }
18                 val = l2->val + flag;
19                 l2 = l2->next;
20             }
21
22             else if(!l2)
23             {
24                 if(flag == 0)
25                 {
26                     t->next = l1;
27                     break;
28                 }
29                 val = l1->val + flag;
30                 l1 = l1->next;
31             }
32
33             else
34             {
35                 val = l1->val + l2->val + flag;
36                 l1 = l1->next;
37                 l2 = l2->next;
38             }
39             flag = val/10;
40             val %= 10;
41             t->next = new ListNode(val);
42             t = t->next;
43         }
44
45         if(flag) t->next = new ListNode(1);
46         return ans->next;
47     }
48 };
时间: 2024-12-29 11:25:28

【LeetCode】002 Add Two Numbers的相关文章

【Leetcode】2. Add Two Numbers

Problem: You are given two non-empty linked lists representing two non-negative integers. 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. You may assume the tw

【Leetcode】445. Add Two Numbers II

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 return it as a linked list. You may assume the two numbers

【Leetcode】2. Add Two Numbers 两数相加

You are given two non-empty linked lists representing two non-negative integers. 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. You may assume the two numbers

【Leetcode】:Compare Version Numbers

题目地址:https://oj.leetcode.com/problems/compare-version-numbers/ 题目描述: Compare two version numbers version1 and version1. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings ar

【Leetcode】Bitwise AND of Numbers Range

题目链接:https://leetcode.com/problems/bitwise-and-of-numbers-range/ 题目: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. 思

【Leetcode】Count of Smaller Numbers After Self

题目链接:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the righ

【LeetCode】258 - Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. Follow up:Could you do it without any

【LeetCode】165 - Compare Version Numbers

Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character.The . characte

【leetcode】Bitwise AND of Numbers Range(middle)

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. 思路: 先找前面二进制相同的位,后面不相同的位相与一定为0. 比如: 1111001 1110011 从0011 - 1001 中间一定会经