当前位置:网站首页>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;
}
边栏推荐
- Arduino uses nrf24l01+ communication
- Zero length array in GNU C
- Charge pump boost principle - this article will give you a simple understanding
- Simple design description of MIC circuit of ECM mobile phone
- Introduction of air gap, etc
- Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine
- Imx6ull bare metal development learning 1-assembly lit LED
- QEMU demo makefile analysis
- Relationship between line voltage and phase voltage, line current and phase current
- Gradle复合构建
猜你喜欢
Explain task scheduling based on Cortex-M3 in detail (Part 2)
H264 (I) i/p/b frame gop/idr/ and other parameters
List of linked lists
[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...)
C WinForm [view status bar -- statusstrip] - Practice 2
Tailq of linked list
Halcon's practice based on shape template matching [1]
Relationship between line voltage and phase voltage, line current and phase current
[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
Record the visual shock of the Winter Olympics and the introduction of the screen 2
随机推荐
C # joint configuration with Halcon
Classic application of MOS transistor circuit design (2) - switch circuit design
【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
Measurement fitting based on Halcon learning [i] fuse Hdev routine
Class of color image processing based on Halcon learning_ ndim_ norm. hdev
Semiconductor devices (I) PN junction
Live555 RTSP audio and video streaming summary (II) modify RTSP server streaming URL address
Beijing Winter Olympics opening ceremony display equipment record 3
UEFI development learning 2 - running ovmf in QEMU
2021-10-28
STM32 virtualization environment of QEMU
Shape template matching based on Halcon learning [viii] PM_ multiple_ models. Hdev routine
Halcon's practice based on shape template matching [1]
Screen record of the opening ceremony of the Beijing winter olympics 2
Why is 1900 not a leap year
Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
matlab timeserise
Shape template matching based on Halcon learning [9] PM_ multiple_ dxf_ models. Hdev routine -- [read and write XLD from DXF file]
Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine
Several important parameters of LDO circuit design and type selection