当前位置:网站首页>Leetcode0002——Add Two Numbers——Linked List
Leetcode0002——Add Two Numbers——Linked List
2022-07-22 20:24:00 【Zheyuan Zou】
This is a topic about one-way linked list , What we are looking at is the construction and traversal of one-way linked lists . The title is as follows :
The traversal of a single linked list takes ordinary addition as the carrier , The meaning of the question is easy to understand .
I wrote two versions of code , One is Opened a new linked list To store the results , One is Add it to the long list in place . It is not difficult to see from the train of thought , The former saves more time ( Because there is no need to judge the length of the linked list , Add and open new nodes directly ), The latter saves more space .
Here is the code of the second solution :
1. First, we need to write a function to get the linked list L1 and L2 The length of , The code is as follows :
// get the length of linked list
int getListLength(ListNode* List)
{
int Length = 0;
ListNode* MeasurePtr = List;
while(MeasurePtr != nullptr)
{
++Length;
MeasurePtr = MeasurePtr->next;
}
return Length;
}
2. This part is AC Core code , Of course, the returned result is a long linked list that stores the final result Longer, The shorter list is used as an addend to add the element values in the longer list one by one . Another two pointers are opened , One day ago (Lead) After one (Follow) To traverse the long linked list . This is because if the previous pointer has pointed to the last linked list node, it will jump out of the loop , And then There's also rounding that hasn't been handled , At this time, we need to follow Follow Pointer to apply for a new linked list node To store the carry of the highest bit , This is the meaning of the following code :
// if there is a remained carry bit
if(Carry)
Follow->next = new ListNode(Carry);
Here is the complete code
// in-place calculation
ListNode* Longer = getListLength(l1) >= getListLength(l2) ? l1 : l2;
ListNode* Shorter = Longer == l1 ? l2 : l1;
ListNode* Lead = Longer;
ListNode* Follow;
// carry bit
int Carry = 0;
while(Lead != nullptr)
{
// add the shorter list's value
if(Shorter != nullptr)
Lead->val += Shorter->val;
// add the carry bit
Lead->val += Carry;
// if the Result > 10
if(Lead->val >= 10)
{
Lead->val %= 10;
Carry = 1;
}
else
Carry = 0;
// modify pointers
Follow = Lead;
Lead = Lead->next;
if(Shorter != nullptr)
Shorter = Shorter->next;
}
// if there is a remained carry bit
if(Carry)
Follow->next = new ListNode(Carry);
return Longer;
边栏推荐
- dns劫持如何完美修复?dns被劫持如何解决如何完美修复
- euler五十讲(一)
- Spark资料查找
- Microblaze添加自定义IP核,挂AXI总线实现SSD1306 OELD驱动
- Elastic Search 学习入门之ES的简单操作命令(二)
- LeetCode21——Merge two sorted lists——Iteration or recursion
- LeetCode0003——longest substring without repeating characters——Sliding Window
- Elastic Search 学习入门之插件安装(五)
- yum安装提示保护多库版本
- Xilinx FPGA Microblaze AXI_IIC使用方法及心得
猜你喜欢
随机推荐
在使用cv2实现人脸识别时在识别框上显示中文
The well-known software adsafe conceals malicious code to hijack traffic from many websites
LeetCode160 & LeetCode141——double pointers to solve the linked list
LeetCode103——zigzagLevelOrder of binary tree
vim 使用tips
How to solve the blue screen problem in win8.1 system and how to solve the malicious hijacking of IE home page?
Redis accesses JSON data
Statistical analysis of offline log collection
LeetCode21——Merge two sorted lists——Iteration or recursion
LeetCode0022——括号生成——DFS
达梦数据库安装使用避坑指南
The database is encapsulated by queryrunner simulation
What does Baidu snapshot hijacking mean? How to solve Baidu snapshot hijacking and Baidu hijacking
百度快照劫持是什么意思?如何解决百度快照被劫持、百度劫持
druid加密命令
Nc26 bracket generation
Chinese search for introduction to elastic search (8)
百度首页被tn劫持的办法有那些、两种解决百度劫持的方法
LeetCode146——LRU Cache——DS Design
What are the common ways for websites to be hacked and hijacked? What are the DNS hijacking tools?