当前位置:网站首页>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;
}
边栏推荐
- Take you to understand the working principle of lithium battery protection board
- 剑指 Offer 06. 从尾到头打印链表
- Wifi-802.11 negotiation rate table
- Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine
- Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine
- Carrier period, electrical speed, carrier period variation
- 实例006:斐波那契数列
- Compilation warning solution sorting in Quartus II
- Problem solving: interpreter error: no file or directory
- Bluetooth hc-05 pairing process and precautions
猜你喜欢

List of linked lists

NTC thermistor application - temperature measurement

Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)

Summary -st2.0 Hall angle estimation

OC and OD gate circuit

Hardware 1 -- relationship between gain and magnification

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

UE像素流,来颗“减肥药”吧!

Installation and use of libjpeg and ligpng
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?
随机推荐
【三层架构】
Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
STM32 --- serial port communication
Class of color image processing based on Halcon learning_ ndim_ norm. hdev
[tutorial 19 of trio basic from introduction to proficiency] detailed introduction of trio as a slave station connecting to the third-party bus (anybus PROFIBUS DP...)
Sizeof (function name) =?
Buildroot system for making raspberry pie cm3
[trio basic from introduction to mastery tutorial XIV] trio realizes unit axis multi-color code capture
Problem solving: interpreter error: no file or directory
OC and OD gate circuit
On boost circuit
STM32 --- NVIC interrupt
Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
STM32 --- GPIO configuration & GPIO related library functions
Tailq of linked list
Halcon's practice based on shape template matching [1]
实例009:暂停一秒输出
Charge pump boost principle - this article will give you a simple understanding
亿学学堂给的证券账户安不安全?哪里可以开户
Sql Server的存储过程详解