当前位置:网站首页>Sword finger offer special assault edition day 8
Sword finger offer special assault edition day 8
2022-07-24 07:31:00 【hys__ handsome】
The finger of the sword Offer II 024. Reverse a linked list
Iterative reverse linked list
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *pre = nullptr, *tmp, *ptr = head;
while(ptr != nullptr) {
tmp = ptr;
ptr = ptr->next;
tmp->next = pre;
pre = tmp;
}
return pre;
}
};
Recursive version ( Slower than iteration )
class Solution {
public:
void fun(ListNode* now,ListNode* pre,ListNode *(&res)) {
if(now == nullptr) {
res = pre;
return;
} else {
fun(now->next,now,res);
now->next = pre;
}
}
ListNode* reverseList(ListNode* head) {
ListNode *res = nullptr;
fun(head,nullptr,res);
return res;
}
};
The finger of the sword Offer II 025. The two numbers in the linked list are added
Similar to high-precision addition , Iterative inversion linked list is used
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *pre = nullptr, *tmp, *ptr = head;
while(ptr != nullptr) {
tmp = ptr;
ptr = ptr->next;
tmp->next = pre;
pre = tmp;
}
return pre;
}
ListNode* add(ListNode *head1, ListNode *head2) {
ListNode *res = new ListNode(0), *ptr = res;
int t = 0;
while(head1 != nullptr || head2 != nullptr || t != 0) {
int sum = t;
if(head1 != nullptr) {
sum += head1->val;
head1 = head1->next;
}
if(head2 != nullptr) {
sum += head2->val;
head2 = head2->next;
}
t = sum/10;
ptr->next = new ListNode(sum%10);
ptr = ptr->next;
}
res = res->next;
return reverseList(res);
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
auto head1 = reverseList(l1);
auto head2 = reverseList(l2);
return add(head1,head2);
}
};
The finger of the sword Offer II 026. Rearrange the list
Method 1 、 Linear table storage , Make it possible to randomly access
class Solution {
public:
void reorderList(ListNode* head) {
vector<ListNode*> list;
ListNode *ptr = head;
while(ptr != nullptr) {
list.emplace_back(ptr);
ptr = ptr->next;
}
for(auto ptr:list) ptr->next = nullptr;
int n = list.size();
for(int i = 0; i < n/2; i++) {
list[i]->next = list[n-i-1];
if(n-i-1 > i+1) list[n-i-1]->next = list[i+1];
}
}
};
Method 2 、 Look for the point in the list + Reverse order of linked list + Merge list ( Knowledge points are more comprehensive )
Note that the target linked list is the result of combining the left half end of the original linked list and the right half end after inversion .
In this way, our task can be divided into three steps :
- Find the midpoint of the original list ( Reference resources 「876. The middle node of a list 」). We can use the speed pointer to O(N)O(N) Find the middle node of the linked list .
- Reverse the right half of the list ( Reference resources 「206. Reverse a linked list 」). We can use the iterative method to reverse the linked list .
- Merge the two ends of the original linked list . Because the length difference between the two linked lists is no more than 11, Therefore, direct consolidation is sufficient .
class Solution {
public:
void reorderList(ListNode* head) {
if (head == nullptr) {
return;
}
ListNode* mid = middleNode(head);
ListNode* l1 = head;
ListNode* l2 = mid->next;
mid->next = nullptr;
l2 = reverseList(l2);
mergeList(l1, l2);
}
ListNode* middleNode(ListNode* head) {
ListNode* slow = head;
ListNode* fast = head;
while (fast->next != nullptr && fast->next->next != nullptr) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
ListNode* reverseList(ListNode* head) {
ListNode* prev = nullptr;
ListNode* curr = head;
while (curr != nullptr) {
ListNode* nextTemp = curr->next;
curr->next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
void mergeList(ListNode* l1, ListNode* l2) {
ListNode* l1_tmp;
ListNode* l2_tmp;
while (l1 != nullptr && l2 != nullptr) {
l1_tmp = l1->next;
l2_tmp = l2->next;
l1->next = l2;
l1 = l1_tmp;
l2->next = l1;
l2 = l2_tmp;
}
}
};
边栏推荐
- Decompress the anchor and enjoy 4000w+ playback, adding a new wind to the Kwai food track?
- Influxdb unauthorized access & CouchDB permission bypass
- JS_实现多行文本根据换行分隔成数组
- DOM operation of JS -- style operation
- 【HiFlow】腾讯云HiFlow场景连接器实现校园信息管理智能化
- oracle中有A,B连个表,这两个表需要第三个表C关联,那怎么将A表中的字段MJ1更新为B表中MJ2的值
- Vulnhub DC1
- Part II - C language improvement_ 2. Memory partition
- Who can stand it when the project goes online
- QoS quality of service three DiffServ Model message marking and PHB
猜你喜欢

Customization or GM, what is the future development trend of SaaS in China?

Feature Selective Anchor-Free Module for Single-Shot Object Detection

R语言手写数字识别

Service Vulnerability & FTP & RDP & SSH & Rsync

Learning strategies of 2D target detection overview (final chapter)

Pytorch deep learning practice lesson 10 / assignment (basic CNN)

服务漏洞&FTP&RDP&SSH&rsync

17. What is the situation of using ArrayList or LinkedList?

PHP escape string

Filter filter
随机推荐
Introduction to C language I. branch and loop statements
OpenCascade笔记:gp包
Who can stand it when the project goes online
Problems encountered in inserting large quantities of data into the database in the project
二维平面多段线Y轴最短距离
【C语言入门】ZZULIOJ 1011-1015
QoS quality of service three DiffServ Model message marking and PHB
单场GMV翻了100倍,冷门品牌崛起背后的“通用法则”是什么?
stdafx. H introduction and function
PHP escape string
Bookkeeping app: xiaoha bookkeeping 2 - production of registration page
SPI - send 16 bit and 8-bit data
服务漏洞&FTP&RDP&SSH&rsync
stdafx.h 简介及作用
Jackson parsing JSON detailed tutorial
使用堡垒机(跳板机)登录服务器
Compilation and debugging (GCC, g++, GDB)
Learning notes - distributed transaction theory
php 转义字符串
归纳、概括、演绎