LeetCode-Medium-Add Two Number

###Add Two Number
本来是一道非常简单的题目原题目的意思是,给两个链表,两个链表都链尾是十进制的头部
例如有链表形式 9->8->7 对应的十进制就是789,而后的操作时将两个链表相加并且合并到一个链表里面

###第一次的解法
我第一次是被结构体卡住了,才考研完,很多基础知识全忘了,比如构造函数,与函数的返回值以及传参的问题,导致自己被这个结构体卡了半天,所以第一个解法就没有用leetcode给的结构体,而是自己的结构体,显然是不会通过leetcode只是为了自己理清思路。
```cpp
//LeetCode struct
struct ListNode{
    int val;
    ListNode *next;
    ListNode(int x):val(x),next(nullptr){}
};

//my self struct
struct ListNode{
    int val;
    ListNode *next;
};
```

可以看到两个结构体其实就是一个构造函数的区别,一个是要在初始化时通过显式构造函数,一个是默认构造函数,而我这个二五仔居然一开始没想起来。
下面贴出实现代码(太过冗长)
```cpp
class Solution {
public:
 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
  ListNode *p = l1;
  ListNode *q = l2;//equation
  int len1 = 0, len2 = 0, temp = 0;
  int Maxlen = 0, Minlen = 0;
  while (p->next!=nullptr)
  {
   p = p->next;
   ++len1;
  }
  while (q->next!=nullptr)
  {
     q= q->next;
   ++len2;
  }//find length of the linklist
  p = l1->next;
  q = l2->next;
  if (len1 >= len2) {
   Maxlen = len1; Minlen = len2;
   while (Minlen--)
   {
    temp = p->val + q->val;
    if (temp >= 10) {
     p->val = temp % 10;
     if (p->next == nullptr) {
      ListNode *s = new ListNode;
      p->next = s; p = s;
      p->val = 1;
      p->next = nullptr;
      q = q->next;
     }
     else {
      p = p->next;
      ++p->val;
      q=q->next;
     }
    }
    else
    {
     p->val = temp;
     p = p->next;
     q = q->next;
    }
    
   }
   return l1;
  }
  else if (len1 <len2) {
   Maxlen = len2; Minlen = len1;
   while (Minlen--)
   {
    temp = p->val + q->val;
    if (temp >= 10) {
     q->val = temp % 10;
     if (p->next == nullptr) {
      ListNode *s = new ListNode;
      q->next = s; q = s;
      q->val = 1;
      q->next = nullptr;
      p = p->next;
     }
     else
     {
      q = q->next;
      ++q->val;
      p = p->next;
     }
    }
    else
    {
     q->val = temp;
     p = p->next;
     q = q->next;
    }

}
   return l2;
  }
 }

```
这第一次的代码真的是又臭又长,我通过判断当前的链表长度,然后再依据长度来进行主链表的加法,真的是又臭又长,完全没想到利用链表本身的特性,即遍历到尾节点后遇到空就可以用判断语句停止。真的跟猪一样我。
于是痛定思痛,手写了一下代码,再去看了下优秀的代码就清爽多了(手写代码真的很不错)

```cpp
class Solution {
public:
 ListNode* addTwoNumbers(ListNode* L1, ListNode* L2) {
  ListNode preHead(0);
  ListNode *p = &preHead;
  int extra = 0;
  while (L1 || L2 || extra) {
   if (L1) {
    extra += L1->val;
    L1 = L1->next;
     }
   if (L2) {
    extra += L2->val;
    L2 = L2->next;
   }//two Link try to add self value to extra,in order to soon afterwards compile
   p->next =new ListNode(extra % 10);
   extra = extra / 10;
   p = p->next;
   //above three procedure already complete pastorder insert;
  }
  return preHead.next;
 }
 
};
```
代码思想是,将每个链表对应的值,加到一个临时变量上,再通过临时变量对新节点进行赋值,在这时对劲新节点进行尾插法,因为考研复习了英语,所以注释都是英文,当然语法我都不在意了,我英语巨辣鸡仅限于我读得懂。(也是锻炼自己吧)。

原文地址:https://www.cnblogs.com/Yekko/p/12146686.html

时间: 2024-11-09 04:03:55

LeetCode-Medium-Add Two Number的相关文章

Leetcode #2 Add two number

Q: 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 -&g

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

【LeetCode】Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". public class Solution { public String addBinary(String a, String b) { if(a.equalsIgnoreCase("")||a==null) r

leetcode第一刷_Valid Number

又是那种看上去非常简单,但非常难过的问题,主要是所有的测试用例很难考虑全面.下面我列举出所有的情况: 1. 字符串前面是可以含有空格的,但是全部都是空格是不行的. 2. 一个数字开头可以是哪些字符呢?很容易想到的是"+"和"-",但是别忘记还有小数点. 3. 一个数字中间也是可以含有字符的,比如科学计数法的"e",而且e之后是可以接"+"或者"-"的.但是要注意e是不是作为数字的开头或者结尾的. 4. 一个

LeetCode:Add Two Numbers - 两个链表逐项做带进位的加法生成新链表

1.题目名称 Add Two Numbers (两个链表逐项做带进位的加法生成新链表) 2.题目地址 https://leetcode.com/problems/add-two-numbers/ 3.题目内容 英文: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

LeetCode:Add Digits - 非负整数各位相加

1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits 3.题目内容 英文:Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. 中文:有一个非负整数num,重复这样的操作:对该数字的各位数字求和,对这个和的各位数字再求和--直到最后得到一个仅1位的数字

leetcode第一刷_Single Number II

其他出现两次,只有一个出现一次的那道题我就不更了,直接抑或,最后的结果就是那个数.为什么可以这样做呢?因为一个32位int,如果所有数都出现了两次,那么为1的那些位统计的个数一定是2的倍数,抑或之后全变成0.一个数出现了一次,它为1的那些位上,1的个数必定是奇数,抑或之后一定还是1. 我之前知道出现两次这个题的解法,但是理解的不够深,以为抑或是关键,其实不是,出现了偶数次才是关键.理解了这点,推广到出现3次上,如果所有的出现了三次,那么为1的那些位1的个数一定是三的倍数,那如果有一个数出现了一次

Leetcode 位运算 Single Number

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Single Number Total Accepted: 20063 Total Submissions: 44658 Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear

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