当前位置:网站首页>leetcode - 445. Add two numbers II
leetcode - 445. Add two numbers II
2022-07-05 08:22:00 【zmm_ mohua】
leetcode - 445. Addition of two numbers II
subject
Code
#include <iostream>
#include <vector>
using namespace std;
typedef struct ListNode{
int val;
struct ListNode *next;
}ListNode, *LinkList;
void create(LinkList &head){
int n;
cin>>n;
head = new ListNode;
head->next = NULL;
ListNode *tail = head;
for(int i = 0; i < n; i++){
ListNode *p = new ListNode;
cin>>p->val;
p->next = NULL;
tail->next = p;
tail = p;
}
}
// With arrays
ListNode* addTwoNumbers1(ListNode* l1, ListNode* l2) {
vector<int> nums1, nums2, res;
while(l1){
nums1.push_back(l1->val);
l1 = l1->next;
}
while(l2){
nums2.push_back(l2->val);
l2 = l2->next;
}
int i = nums1.size() - 1;
int j = nums2.size() - 1;
int flag = 0, sum = 0, num = 0;
while(i >= 0 || j >= 0){
if(i < 0){
sum = flag + nums2[j--];
}else if(j < 0){
sum = flag + nums1[i--];
}else{
sum = nums1[i--] + nums2[j--] + flag;
}
flag = sum / 10;
num = sum % 10;
res.push_back(num);
}
if(flag){
res.push_back(flag);
}
int n = res.size();
ListNode *head = new ListNode;
head->next = NULL;
ListNode *tail = head;
for(int k = n - 1; k >= 0; k--){
ListNode *p = new ListNode;
p->val = res[k];
p->next = NULL;
tail->next = p;
tail = p;
}
return head->next;
}
// List reversal
ListNode* reverse(ListNode *head){
ListNode *pre = new ListNode;
pre = NULL;
ListNode *cur = head;
while(cur){
ListNode *temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
return pre;
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *h1 = reverse(l1);
ListNode *h2 = reverse(l2);
ListNode *head = new ListNode;
head->next = NULL;
ListNode *tail = head;
int sum = 0, flag = 0, num = 0;
while(h1 || h2){
if(!h1){
sum = flag + h2->val;
h2 = h2->next;
}else if(!h2){
sum = flag + h1->val;
h1 = h1->next;
}else{
sum = flag + h1->val + h2->val;
h1 = h1->next;
h2 = h2->next;
}
flag = sum / 10;
num = sum % 10;
ListNode *p = new ListNode;
p->val = num;
p->next = NULL;
tail->next = p;
tail = p;
}
if(flag){
ListNode *p = new ListNode;
p->val = flag;
p->next = NULL;
tail->next = p;
tail = p;
}
head = head->next;
return reverse(head);
}
int main(){
ListNode *l1, *l2, *res;
create(l1);
create(l2);
l1 = l1->next;
l2 = l2->next;
res = addTwoNumbers(l1, l2);
while(res){
cout<<res->val<<" ";
res = res->next;
}
return 0;
}
边栏推荐
- [nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)
- Explication de la procédure stockée pour SQL Server
- WiFi wpa_ Detailed description of supplicant hostpad interface
- 99 multiplication table (C language)
- Soem EtherCAT source code analysis II (list of known configuration information)
- Soem EtherCAT source code analysis I (data type definition)
- Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev
- Synchronization of QT multithreading
- Weidongshan Internet of things learning lesson 1
- Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
猜你喜欢
Count the number of inputs (C language)
OC and OD gate circuit
[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
Example 002: the bonus paid by the "individual income tax calculation" enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increase
STM32 single chip microcomputer -- volatile keyword
【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
STM32 --- serial port communication
MHA High available Cluster for MySQL
Arduino uses nrf24l01+ communication
随机推荐
[trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
关于线性稳压器的五个设计细节
[paper reading] the latest transfer ability in deep learning: a survey in 2022
List of linked lists
Sizeof (function name) =?
Several important parameters of LDO circuit design and type selection
On boost circuit
Shape template matching based on Halcon learning [viii] PM_ multiple_ models. Hdev routine
STM32 single chip microcomputer -- volatile keyword
Let's briefly talk about the chips commonly used in mobile phones - OVP chips
OLED 0.96 inch test
Adaptive filter
FIO测试硬盘性能参数和实例详细总结(附源码)
MySQL MHA high availability cluster
Verilog -- state machine coding method
Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine
Sql Server的存儲過程詳解
实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
Explain task scheduling based on Cortex-M3 in detail (Part 2)