当前位置:网站首页>Niuke.com brush questions (1)
Niuke.com brush questions (1)
2022-07-31 21:36:00 【std i hurt o love】
重排链表
解法一:暴力
Looking at the sorted sequence we can see that,The first node is followed by the last node,The second node is followed by the second-to-last node,以此类推.So we can find the last node every time and connect it after the current node
我们用一个指针p指向头节点,Each time find the last node of the linked list and join itpbehind the pointed node,然后p再指向下一个节点,直到pStop when the number of subsequent nodes is less than two
class Solution {
public:
void reorderList(ListNode *head) {
ListNode* p = head;
while(p){
if(p->next && p->next->next){
// 若p后面至少有两个节点
ListNode *pre = p->next, *cur = p->next->next;
while(cur->next){
// 找到最后一个节点cur, pre指向倒数第二个节点
cur = cur->next;
pre = pre->next;
}
// 将cur接在p后面
pre->next = nullptr;
cur->next = p->next;
p->next = cur;
p = p->next->next;
}
else break;
}
}
};
时间复杂度:O(n^2), n为节点数,The time complexity of finding the last one each time is O(n),故总时间复杂度为O(n^2)
空间复杂度:O(1)
解法二:(线性表)
The disadvantage of method 1 is that the linked list cannot access elements through subscripts,Therefore, it takes time to find the last node each time,So we thought that a linear table could be used to store node pointers,Then you can reconnect the linked list
实现细节:We start by putting each node pointer into an array,Then use the double pointer algorithm to take out the two corresponding nodes before and after each time and let the previous node point to the next node,然后用一个指针preRepresents the tail node pointer of the previous section,若pre不为空,There is a linked list in front of the description,让preJust point to the previous node taken out,再令preis the last node taken out,以此类推,最后记得让pre指向空,当然前提是pre自身不为空
class Solution {
public:
void reorderList(ListNode *head) {
vector<ListNode*> vec; // 创建线性表
ListNode* cur = head;
while(cur){
// Put the node pointer into the linear table
vec.emplace_back(cur);
cur = cur->next;
}
ListNode* pre = nullptr;
int i = 0, j = vec.size() - 1;
while(i <= j){
// Reconnect the linked list using two-pointer arithmetic
vec[i]->next = vec[j];
if(pre) pre->next = vec[i];
pre = vec[j];
i++, j--;
}
if(pre) pre->next = nullptr; // The tail node points to a null pointer
}
};
时间复杂度:O(n), n为链表节点数,The time complexity of the double pointer arithmetic is O(n)
空间复杂度:O(n), Space used by linear tables
解法三:(快慢指针、链表翻转、链表合并)
We can first find the midpoint of the linked list through the fast and slow pointers,Then split the linked list in two,Then flip the second segment of the linked list,Finally, the two linked lists can be crossed and merged into one
This approach involves finding the midpoint of the linked list with the fast and slow pointers,In-place flipping of linked lists and cross-merging of linked lists,综合性很强,The proficiency and basic skills of our pointer use were examined
为了方便理解,I use animations to help you understand the basic implementation principles,Algorithms for in-place flipping of linked lists,I'll demonstrate with a gif at the end
class Solution {
public:
void reorderList(ListNode* head) {
if(!head) return;
ListNode* mid = GetMid(head); // 得到链表的中点
ListNode *p1 = head, *p2 = mid->next;
mid->next = nullptr; // 断开链表
p2 = ReverseList(p2); // Flip the last linked list
MergeList(p1, p2); // Cross-merge two linked lists
}
ListNode* GetMid(ListNode* head) {
// Return to the midpoint of the linked list
ListNode *slow = head, *fast = head;
while(fast->next && fast->next->next){
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
ListNode* ReverseList(ListNode* head) {
// In-place flip of the linked list
if(!head) return head;
ListNode *pre = head, *cur = head->next;
while(cur){
head->next = cur->next;
cur->next = pre;
pre = cur;
cur = head->next;
}
return pre;
}
void MergeList(ListNode *l1, ListNode *l2) {
// Cross-merge two linked lists
ListNode *tmp1, *tmp2;
while(l1 && l2){
tmp1 = l1->next;
tmp2 = l2->next;
l1->next = l2;
l2->next = tmp1;
l1 = tmp1;
l2 = tmp2;
}
}
};
时间复杂度:O(n),快慢指针,链表合并,The time complexity of flipping a linked list is bothO(n)的
空间复杂度:O(1)
边栏推荐
- 顺序表的实现
- Shell script quick start to actual combat -02
- ThreadLocal
- Bika LIMS open source LIMS set - use of SENAITE (detection process)
- 高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询
- leetcode 665. Non-decreasing Array 非递减数列(中等)
- 1161. Maximum Sum of Elements in Layer: Hierarchical Traversal Application Problems
- Realize serial port receiving data based on STM32 ring queue
- 第六章
- &lt;artifactId&gt;ojdbc8&lt;/artifactId&gt;「建议收藏」
猜你喜欢
[Intensive reading of the paper] iNeRF
Socket Review and I/0 Model
IDA PRO中汇编结构体识别
关注!海泰方圆加入《个人信息保护自律公约》
[NLP] What is the memory of the model!
Poker Game in C# -- Introduction and Code Implementation of Blackjack Rules
Student management system on the first day: complete login PyQt5 + MySQL5.8 exit the operation logic
ThreadLocal
嵌入式开发没有激情了,正常吗?
Embedded development has no passion, is it normal?
随机推荐
Redis综述篇:与面试官彻夜长谈Redis缓存、持久化、淘汰机制、哨兵、集群底层原理!...
Basics of ResNet: Principles of Residual Blocks
Book of the Month (202207): The Definitive Guide to Swift Programming
Poker Game in C# -- Introduction and Code Implementation of Blackjack Rules
移动web开发02
返回一个零长度的数组或者空的集合,不要返回null
rj45对接头千兆(百兆以太网接口定义)
Several methods for deleting specified elements in Golang slices
Go mode tidy reports an error go warning “all” matched no packages
Count characters in UTF-8 string function
【愚公系列】2022年07月 Go教学课程 023-Go容器之列表
利用反射实现一个管理对象信息的简单框架
【Yugong Series】July 2022 Go Teaching Course 023-List of Go Containers
Three.js入门
关注!海泰方圆加入《个人信息保护自律公约》
Transfer Learning - Domain Adaptation
21. Support Vector Machine - Introduction to Kernel Functions
Redis Overview: Talk to the interviewer all night long about Redis caching, persistence, elimination mechanism, sentinel, and the underlying principles of clusters!...
The whole network is on the verge of triggering, and the all-round assistant for content distribution from media people - Rongmeibao
pytorch lstm时间序列预测问题踩坑「建议收藏」