今天看不进去论文,也学不进去新技术,于是先把题刷了,一会补别的。

-----------------------------------------------------我才不是分割线-------------------------------------------------

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 -> 4)
Output: 7 -> 0 -> 8

【题意】:就是给你两个链表,链表内的内容为一个多位数的逆序,比如342就表示为(2 -> 4 -> 3),现在让你算这两个多位数的和,

同时用相同的方法表示出来,也就是将和的逆序用链表表示出来。

【心路历程】看到题目一开始的想法就是考查模拟加法+链表操作的题,还算简单,链表操作就是一个尾部加链表的方法。

于是开始码代码,写完一交发现出现RE (TAT)。错误的样例为[0],[0]。想了半天不知道哪里错了。。。

后来发现我head指针没分配空间,额额额,改完一交,AC (^ ^)

------------------------------------------------------------------------------------------------------------------------

代码如下:

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode * res = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode * ans = NULL;
int judge = ;
int save = ;
int add1,add2,sum;
if(l1 == NULL && l2 == NULL) return NULL;
while(l1 != NULL || l2 != NULL) {
if(l1 == NULL) {
add1 = ;
add2 = l2->val;
l2 = l2->next;
}else if(l2 == NULL){
add1 = l1 ->val;
add2 = ;
l1 = l1->next;
}else {
add1 = l1->val;
add2 = l2->val;
l1 = l1->next;
l2 = l2->next;
}
sum = add1 + add2 + save;
save = sum / ;
struct ListNode * temp = (struct ListNode *)malloc(sizeof(struct ListNode));
temp->val = sum % ;
temp->next = NULL;
res->next = temp;
if(judge == ) {
ans = temp;
judge = ;
}
res = res->next;
}
if(save){
struct ListNode * temp = (struct ListNode *)malloc(sizeof(struct ListNode));
temp->val = save;
temp->next = NULL;
res->next = temp;
}
return ans;
}

一起刷LeetCode2-Add Two Numbers的更多相关文章

  1. Leetcode-2 Add Two Numbers

    #2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...

  2. leetcode2:Add Two Numbers

    Add Two Numbers Total Accepted: 55216 Total Submissions: 249950My Submissions You are given two link ...

  3. Leetcode2:Add Two Numbers@Python

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  4. leetcode-2 Add Two Numbers 计算两个对应的列表和问题

     1.问题描写叙述: You are given two linked lists representing two non-negativenumbers. The digits are sto ...

  5. LeetCode-2. Add Two Numbers(链表实现数字相加)

    1.题目描述 You are given two non-empty linked lists representing two non-negative integers. The digits a ...

  6. Leetcode2.Add Two Numbers两数相加

    给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...

  7. LeetCode 2. 两数相加(Add Two Numbers)

    2. 两数相加 2. Add Two Numbers 题目描述 You are given two non-empty linked lists representing two non-negati ...

  8. (python)leetcode刷题笔记 02 Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

  9. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  10. 【LeetCode445】 Add Two Numbers II★★

    题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...

随机推荐

  1. Quartz的misfire特性

    Quartz的misfire特性 只有一个线程.多个job 第一个job产生misfire(executeTime>Interval) 且是repeatForever 那么只会运行第一个job, ...

  2. Spring的父子容器问题

    在ssm框架搭建的时候 配置了一个Spring容器,又配置了一个前端控制器 <!-- 初始化spring容器 --> <context-param> <param-nam ...

  3. C++:成员运算符重载函数和友元运算符重载函数的比较

    5.2.4 成员运算符重载函数和友元运算符重载函数的比较 (1)对双目运算符而言,成员运算符重载函数参数表中含有一个参数,而友元运算符重载函数参数表中有两个参数:对于单目运算符而言,成员运算符重载函数 ...

  4. Java API —— 多线程

    1.多线程概述     1)进程:         正在运行的程序,是系统进行资源分配和调用的独立单位.         每一个进程都有它自己的内存空间和系统资源.     2)线程:         ...

  5. How to Determine the Version of Oracle XML Publisher for Oracle E-Business Suite 11i and Release 12 (Doc ID 362496.1)

    Modified: 29-Mar-2014 Type: HOWTO In this DocumentGoal   Solution   1. Based upon an output file gen ...

  6. HDU 1754 I Hate It (线段树 单点更新)

    题目链接 中文题意,与上题类似. #include <iostream> #include <cstdio> #include <cstring> #include ...

  7. uva 1642 Magical GCD

    很经典的题目,愣是没做出来.. 题意:给出一个序列,求一子序列,满足其GCD(子序列)* length(子序列)最大. 题解: 类似单调队列的思想,每次将前面所得的最大公约数与当前数进行GCD,若GC ...

  8. 函数buf_page_init

    /********************************************************************//** Inits a page to the buffer ...

  9. bzoj1564

    嗯,这是一道简单题 注意二叉搜索树的子树中序一定是连续的 又因为取值修改是任意的并且修改代价与权值无关 不难想到把权值离散化,然后按找数据值排序,然后dp f[i][j][w]表示从i~j的节点构成一 ...

  10. 学军NOI训练13 T3 白黑树

    唉,大学军有自己的OJ就是好,无限orz 只有周六的比赛是开放的囧,这场比赛最后因为虚拟机卡住没有及时提交…… 否则就能让大家看到我有多弱了…… 前两题题解写的很详细,可以自己去看,我来随便扯扯T3好 ...