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

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

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. iOS开发--_weak typeof(self) weakSelf = self

    _weak typeof(self) weakSelf = self;  (一)内存管理原则  1.默认strong,可选weak.strong下不管成员变量还是property,每次使用指针指向一个 ...

  2. Executing Raw SQL Queries using Entity Framework

    原文 Executing Raw SQL Queries using Entity Framework While working with Entity Framework developers m ...

  3. vss的ss.ini丢失或损坏导致的vss无法登录错误

    vss的ss.ini丢失或损坏导致的vss无法登录错误 Written in 2007-07-03 18:17 在vss使用过程中,不知道什么原因,会导至vss目录中的ss.ini文件损坏,此文件位于 ...

  4. IntelliJ IDEA 15开发Java Maven项目

    1.安装好之后开始创建项目

  5. Help And Manual 帮助文件制作工具

    Help And Manual 简    介 帮助文件制作工具 支持文件格式 26种 其他功能 制作非常专业的使用手册 一个所见即所得的帮助文件制作工具,是市面上功能最强的 WYSIWYG (所见即所 ...

  6. 转Java 回调函数的理解

    所谓回调,就是客户程序C调用服务程序S中的某个函数A,然后S又在某个时候反过来调用C中的某个函数B,对于C来说,这个B便叫做回调函数.例如Win32下的窗口过程函数就是一个典型的回调函数.一般说来,C ...

  7. 直接拿来用 九个超实用的PHP代码片段(二)

    每位程序员和开发者都喜欢讨论他们最爱的代码片段,尤其是当PHP开发者花费数个小时为网页编码或创建应用时,他们更知道这些代码的重要性.为了节约编码时间,笔者收集了一些较为实用的代码片段,帮助开发者提高工 ...

  8. 1218. Episode N-th: The Jedi Tournament(bfs)

    1218 简答题 对于当前点 判断每个点是否可达 #include <iostream> #include<cstdio> #include<cstring> #i ...

  9. hashmap的遍历

    import java.util.HashMap;import java.util.Iterator; public class hash { /** * @param args */ public ...

  10. parentNode(返回指定节点的父节点。)

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...