当前位置:网站首页>Add the two numbers in the linked list of the second question of C language. Ergodic method
Add the two numbers in the linked list of the second question of C language. Ergodic method
2022-07-25 00:15:00 【Take care of two dogs and never let them go bad】
Here are two for you Non empty The linked list of , Represents two nonnegative integers . Each of them is based on The reverse Stored in , And each node can only store a Numbers .
Please add up the two numbers , And returns a linked list representing sum in the same form .
You can assume that in addition to the numbers 0 outside , Neither of these numbers 0 start .
Example 1:
Input :l1 = [2, 4, 3], l2 = [5, 6, 4]
Output :[7, 0, 8]
explain :342 + 465 = 807.
Example 2:
Input :l1 = [0], l2 = [0]
Output :[0]
Example 3:
Input :l1 = [9, 9, 9, 9, 9, 9, 9], l2 = [9, 9, 9, 9]
Output :[8, 9, 9, 9, 0, 0, 0, 1]
source : Power button (LeetCode)
link :https ://leetcode.cn/problems/add-two-numbers
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Think of two linked lists as traversal of the same length , If a link list is short, fill it in the front 000, such as 987 + 23 = 987 + 023 = 1010
Each bit needs to consider the carry problem of the previous bit at the same time , The carry value needs to be updated after the current bit calculation
If the two lists are all traversed , Carry value is 111, Then add the node at the front of the new list 111
Tips : For the list problem , When the return result is the header node , Usually you need to initialize a pre pointer first pre, The next node of the pointer points to the real header node head. The purpose of using the pre pointer is that there is no node value available when the list is initialized , And the construction process of linked list needs pointer movement , This will result in the loss of the head pointer , Unable to return a result
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct ListNode
{
int val;
struct ListNode *next;
};
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
int pos = 0;// Check whether to carry , Carry is 1 Non carry is 0
struct ListNode* head = NULL;// Chain header pointer
struct ListNode* tail = NULL;// Pointer at the end of the list
struct ListNode* mid = NULL;//Bianli zhizhen
int s1 = 0, s2 = 0;//s1 representative l1 Corresponding value ,s2 Corresponding l2 Corresponding value
int sum = 0 ;
while (l1 != NULL || l2 != NULL)// Cycle when both linked lists are not at the end
//[5]
//[5]
{
if (l1 == NULL)
s1 = 0;
else if (l1 != NULL)
s1 = l1->val;
if (l2 == NULL)
s2 = 0;
else if (l2 != NULL)
s2 = l2->val;
sum = s1 + s2 + pos;
if (sum>= 10)
pos = 1;
else if (sum < 10)
pos = 0;
if (head == NULL)
{
head = tail = (struct ListNode*)malloc(sizeof(struct ListNode));
head->val = sum % 10;
tail->next = NULL;
}
else if (head != NULL)
{
mid = (struct ListNode*)malloc(sizeof(struct ListNode));
mid->val = sum%10;
mid->next = tail->next;// Use tail insertion
tail->next = mid;
tail = mid;
}
if (l1 != NULL)
l1 = l1->next;
if (l2 != NULL)
l2 = l2->next;
}
if (pos == 1)
{
tail = (struct ListNode*)malloc(sizeof(struct ListNode));
tail->val = pos;
tail->next = NULL;
if (head->next == NULL)// Judge mid Whether there is If mid There is no direct to mid Operation may illegally access
head->next = tail;
else
mid->next = tail;
}
return head;
}
边栏推荐
- codeforces round #797 ABCDEFG
- Pain and happiness -nio programming
- [leetcode weekly replay] 303rd weekly 20220724
- Deep and direct visual slam
- 阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题
- Internal network mapping port to external network
- LeetCode_6124_第一个出现两次的字母
- [nuxt 3] (x) runtime configuration
- Answers to some problems encountered in the process of Xperia XZ (f8332) brushing and root
- 2022 the most NB JVM foundation to tuning notes, thoroughly understand Alibaba P6 small case
猜你喜欢

Development direction and problems of optaplanner

C语言学习之分支与循环语句

4. Immersion test

Quartus: install cyclone 10 LP device library for quartus version 17.1
![[hero planet July training leetcode problem solving daily] 24th line segment tree](/img/ae/1f3288a99cb07fcbb1836357e0229a.png)
[hero planet July training leetcode problem solving daily] 24th line segment tree

Nodejs package
![[mindspore] [xception model] script statement is suspected to be wrong](/img/86/3174f9edadf4b815a76678551cbfa5.jpg)
[mindspore] [xception model] script statement is suspected to be wrong
![[mindspore ascend] [running error] graph_ In mode, run the network to report an error](/img/81/9e96182be149aef221bccb63e1ce96.jpg)
[mindspore ascend] [running error] graph_ In mode, run the network to report an error

SQL file import database - Nanny level tutorial

Efficiency increased by 98%! AI weapon behind operation and maintenance inspection of high altitude photovoltaic power station
随机推荐
Analyzing the principle of DNS resolution in kubernetes cluster
EF core: self referencing organizational structure tree
云计算三类巨头:IaaS、PaaS、SaaS,分别是什么意思,应用场景是什么?
px rem em
阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题
技术操作
2022 the most NB JVM foundation to tuning notes, thoroughly understand Alibaba P6 small case
SQL result export function. If you click the work order but don't enter it, the interface is always blank and there is no response. What should you do?
Paper time review MB2: build a behavior model for autonomous databases
2022 Henan Mengxin League game 2: Henan University of technology K - Rice
R language uses ISNA function to check whether the list and dataframe contain missing values, marks abnormal values in data columns in dataframe as missing values Na, and uses na.omit function to dele
The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode
LeetCode_ 392_ Judgement subsequence
Soft test --- fundamentals of programming language (Part 2)
QT learning - using database singleton to complete login matching + registration function
codeforces round #797 ABCDEFG
dpkg : Breaks: libapt-pkg5.0 (< 1.7~b) but 1.6.15 is to be installedE: Broken packages
Opengauss kernel analysis: query rewriting
91. (leaflet chapter) leaflet situation plotting - offensive direction drawing
Redis memory analysis tool RMA usage