LeetCode Add Two Numbers 两个数相加

 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 public:
11 ListNode *creatnode(int a){
12     ListNode *nod=new ListNode(a);
13     nod->next=0;
14     return nod;
15 }
16 ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
17     if(l1==0)    return l2;
18     if(l2==0)    return l1;
19     struct ListNode *p=l1,*c=0;
20     int tem=0,s=0;
21     while(l1!=0&&l2!=0){    //直到有一边先结束
22         tem=l1->val+l2->val+tem;
23         s=tem%10;            //个位
24         tem=tem/10;            //十位
25         l1->val=s;
26         if(l1->next==0)
27             c=l1;
28         l1=l1->next;
29         l2=l2->next;
30     }
31     if(l1!=0&&l2==0||l1==0&&l2!=0){
32         if(l1==0&&l2!=0){
33             c->next=l2;
34             l1=c->next;
35         }
36         if(tem==0)
37             return p;
38         while(l1!=0){
39             tem=l1->val+tem;
40             l1->val=tem%10;
41             tem=tem/10;
42             if(l1->next==0)
43                 c=l1;
44             l1=l1->next;    //l1可能为NULL
45         }
46         if(tem!=0)
47             c->next=creatnode(tem);
48     }
49     else if(l1==0&&l2==0&&tem!=0){
50         c->next=creatnode(tem);
51     }
52     return p;
53 }
54 };

题意:两个十进制数,反向存储在单向链表里,每位数占一个节点。求和的链表。

思路:将提供的第一条链表作为返回的结果,相加之后的结果存在这条链表中。当l1比l2长时,结果存l1没问题;当l2比l1长时,将l2比l1多出的元素加在l1的后面,这样就不用创建过多的结点了。完全与链表长无关。

注意:考虑两个链表等长和非等长情况,考虑可能出现1+9999,0+0这类情况。

吐槽:先是用节省内存的做法,感觉代码太长。重写出减少代码量的,结果内存超出限制。重新改进节省内存的做法。其实如果再写一个函数用于加法,代码量会更少。 此代码比别人的长了一些~

时间: 2024-10-18 09:22:24

LeetCode Add Two Numbers 两个数相加的相关文章

leetcode——Add Two Numbers 两个链表表示的正整数对其求和(AC)

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] 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 1: 找出两个数相加等于给定数 two sum

问题描述 对于一个给定的数组,找出2个数,它们满足2个数的和等于一个特定的数,返回这两个数的索引.(从1开始) Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target,

[CareerCup] 18.1 Add Two Numbers 两数相加

18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我们实现两数相加,但是不能用加号或者其他什么数学运算符号,那么我们只能回归计算机运算的本质,位操作Bit Manipulation,我们在做加法运算的时候,每位相加之后可能会有进位Carry产生,然后在下一位计算时需要加上进位一起运算,那么我们能不能将两部分拆开呢,我们来看一个例子759+674 1.

【LeetCode】Add Two Numbers(两数相加)

这道题是LeetCode里的第2到题. 这道题的条件判断很简单,如下: 1.是否为尾节点 2.是否产生进位 3.是否等于9 4.是否需要拓展空间 代码如下: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode *p = l1, *q = l2; int add, carry = 0;//carry标志进位 while (1) { add = p->val + q->val + carry; p->val = add

【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

[CareerCup] 2.5 Add Two Numbers 两个数字相加

2..5 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 su

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题 两数相加

/* T2:两数相加 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和. 您可以假设除了数字 0 之外,这两个数都不会以 0 开头. 示例: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807*/ /** * Definition for