一起刷LeetCode2-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) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

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

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

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

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

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

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

代码如下:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     struct ListNode *next;
 6  * };
 7  */
 8 struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
 9     struct ListNode * res = (struct ListNode *)malloc(sizeof(struct ListNode));
10     struct ListNode * ans = NULL;
11     int judge = 0;
12     int save = 0;
13     int add1,add2,sum;
14     if(l1 == NULL && l2 == NULL) return NULL;
15     while(l1 != NULL || l2 != NULL) {
16         if(l1 == NULL) {
17             add1 = 0;
18             add2 = l2->val;
19             l2 = l2->next;
20         }else if(l2 == NULL){
21             add1 = l1 ->val;
22             add2 = 0;
23             l1 = l1->next;
24         }else {
25             add1 = l1->val;
26             add2 = l2->val;
27             l1 = l1->next;
28             l2 = l2->next;
29         }
30         sum = add1 + add2 + save;
31         save = sum / 10;
32         struct ListNode * temp = (struct ListNode *)malloc(sizeof(struct ListNode));
33         temp->val = sum % 10;
34         temp->next = NULL;
35         res->next = temp;
36         if(judge == 0) {
37             ans = temp;
38             judge = 1;
39         }
40         res = res->next;
41     }
42     if(save){
43         struct ListNode * temp = (struct ListNode *)malloc(sizeof(struct ListNode));
44         temp->val = save;
45         temp->next = NULL;
46         res->next = temp;
47     }
48     return ans;
49 }
时间: 2024-10-07 05:29:44

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

[LeetCode-2] 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 ->

LeetCode2——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 Add Two Numbers 计算链表两个对应和的问题

 1.问题描述: You are given two linked lists representing two non-negativenumbers. The digits are stored in reverse order and each of their nodes containa single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 ->

leetcode2 add two numbers

题目为把两个非负整数逆序存储在链表中,求和并返回一个链表.例如:234+126=360,在这个题目中即为 输入:4-->3-->2    6-->2-->1   输出:0-->6-->3

leetcode2 Add Two Numbers题解

题的大概意思就是,输入两个列表,这两个列表是两个逆序的数,比如说1->2->4就代表421.然后将两个链表翻转后相加,存入列表中,同样按照逆序存入列表,将其返回,刚开始题意理解错了,WA了两次,题目给出的一组数据比较具有迷惑性,就是243+564与432+465的结果都是807,所以刚开始我以为输入的两个链表的数正序的,只需将结果翻转就可以了.其实这道题和大整数相加差不太多,只要考虑一下进位就没什么问题了.\ 第一版代码如下,比较繁琐,还有一些测试语句: #include <stdio.

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

2、Add Two Numbers

1.Add Two Numbers--这是leedcode的第二题: 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:

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

Add Two Numbers(Linked List)

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) + (