当前位置:网站首页>leetcode - 445. 两数相加 II
leetcode - 445. 两数相加 II
2022-07-05 08:14:00 【zmm_mohua】
leetcode - 445. 两数相加 II
题目
代码
#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;
}
}
// 借助数组
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;
}
// 链表反转
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;
}
边栏推荐
- Factors affecting the quality of slip rings in production
- FIO测试硬盘性能参数和实例详细总结(附源码)
- STM32 outputs 1PPS with adjustable phase
- Process communication mode between different hosts -- socket
- Ble encryption details
- Step motor generates S-curve upper computer
- Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
- Shell脚本基本语法
- Charge pump boost principle - this article will give you a simple understanding
- UEFI development learning 2 - running ovmf in QEMU
猜你喜欢
Shape template matching based on Halcon learning [viii] PM_ multiple_ models. Hdev routine
DCDC circuit - function of bootstrap capacitor
DokuWiki deployment notes
Wifi-802.11 negotiation rate table
STM32 virtualization environment of QEMU
Let's briefly talk about the chips commonly used in mobile phones - OVP chips
[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
Management and use of DokuWiki (supplementary)
C language enhancement -- pointer
H264 (I) i/p/b frame gop/idr/ and other parameters
随机推荐
Shell脚本基本语法
Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine
NTC thermistor application - temperature measurement
Semiconductor devices (III) FET
[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement
Soem EtherCAT source code analysis II (list of known configuration information)
Shape template matching based on Halcon learning [9] PM_ multiple_ dxf_ models. Hdev routine -- [read and write XLD from DXF file]
C # joint configuration with Halcon
OC and OD gate circuit
How to copy formatted notepad++ text?
Imx6ull bare metal development learning 1-assembly lit LED
Naming rules for FreeRTOS
【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
Adaptive filter
Makefile application
MySQL之MHA高可用集群
Briefly talk about the identification protocol of mobile port -bc1.2
Consul installation
Extern keyword function
Bluetooth hc-05 pairing process and precautions