当前位置:网站首页>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;
}
边栏推荐
- Management and use of DokuWiki (supplementary)
- Hardware 1 -- relationship between gain and magnification
- Arduino uses nrf24l01+ communication
- Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
- OC and OD gate circuit
- STM32---ADC
- Void* C is a carrier for realizing polymorphism
- 【三层架构及JDBC总结】
- matlab timeserise
- Class of color image processing based on Halcon learning_ ndim_ norm. hdev
猜你喜欢

Charge pump boost principle - this article will give you a simple understanding

Count the number of inputs (C language)

More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!

Negative pressure generation of buck-boost circuit

STM32 --- serial port communication

Compilation warning solution sorting in Quartus II

实例002:“个税计算” 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.

C, Numerical Recipes in C, solution of linear algebraic equations, LU decomposition source program

剑指 Offer 06. 从尾到头打印链表

Relationship between line voltage and phase voltage, line current and phase current
随机推荐
Let's briefly talk about the chips commonly used in mobile phones - OVP chips
Introduction of air gap, etc
MHA High available Cluster for MySQL
STM32 virtualization environment of QEMU
99 multiplication table (C language)
STM32 single chip microcomputer - bit band operation
Carrier period, electrical speed, carrier period variation
DCDC circuit - function of bootstrap capacitor
NTC thermistor application - temperature measurement
Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?
Slist of linked list
Stablq of linked list
go依赖注入--google开源库wire
实例009:暂停一秒输出
Adaptive filter
[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine
Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev
Imx6ull bare metal development learning 2- use C language to light LED indicator
Sword finger offer 09 Implementing queues with two stacks