当前位置:网站首页>【刷题笔记】链表内指定区间反转
【刷题笔记】链表内指定区间反转
2022-07-28 23:41:00 【柒海啦】
题目:
描述
将一个节点数为 size 链表 m 位置到 n 位置之间的区间反转,要求时间复杂度 O(n)O(n),空间复杂度 O(1)O(1)。
例如:
给出的链表为 1\to 2 \to 3 \to 4 \to 5 \to NULL1→2→3→4→5→NULL, m=2,n=4m=2,n=4,
返回 1\to 4\to 3\to 2\to 5\to NULL1→4→3→2→5→NULL.
数据范围: 链表长度 0 < size \le 10000<size≤1000,0 < m \le n \le size0<m≤n≤size,链表中每个节点的值满足 |val| \le 1000∣val∣≤1000
要求:时间复杂度 O(n)O(n) ,空间复杂度 O(n)O(n)
进阶:时间复杂度 O(n)O(n),空间复杂度 O(1)O(1)
示例1
输入:
{1,2,3,4,5},2,4返回值:
{1,4,3,2,5}示例2
输入:
{5},1,1返回值:
{5}
解答:
此题只要掌握了反转链表一般就能解的出来,至于给的这个区间只需要遍历到此处即可。
我们的目的是在遍历的同时将整个链表反转了。那么我们是不是可以将一开始的节点固定住,然后每次循环的时候,只需将此固定节点后一个移动到固定节点前面,让固定节点前面一个固定节点指向此移动节点,然后移动节点指向一开始前固定节点的下一个。(一开始指向第一个节点,然后就是指向第二个,这样取的时候就相当于在前固定节点和固定节点中重新排了个倒序)。这样就可一移动相差多少步即可。
前固定节点为res,固定节点是cur。为了防止是一开始从第一个就开始排,那么这个前固定节点就需要创建一个头节点,值随意,然后指向头,这样返回的时候返回其指向的头即可。
然后通过循环即i < m找到最开始的那个节点,res就等于此节点前一个,那么cur就是指向后一个。
代码:
struct ListNode* reverseBetween(struct ListNode* head, int m, int n ) {
// write code here
struct ListNode* res = (struct ListNode*)malloc(sizeof(struct ListNode));
res->next = head;
struct ListNode* pre = res;
struct ListNode* cur = head;
for (int i = 1; i < m; i++)
{
pre = cur;
cur = cur->next;
}
for(int i = m; i < n; i++)
{
struct ListNode* temp = cur->next;
cur->next = temp->next;
temp->next = pre->next;
pre->next = temp;
}
return res->next;
}边栏推荐
- The method of tracking the real-time market of London Silver
- Talk about the cross end technical scheme
- 时间序列数据的预处理方法总结
- 返回*this的成员函数
- DRF - deserialization of serializer, fields and parameters, local and global hooks, use of modelserializer
- 【愚公系列】2022年07月 Go教学课程 020-Go容器之数组
- Yield Guild Games:这一年的总结与未来展望
- Have you seen the management area decoupling architecture? Can help customers solve big problems
- mysql时间按小时格式化_mysql时间格式化,按时间段查询的MySQL语句[通俗易懂]
- Common sparse basis and matlab code for compressed sensing
猜你喜欢

Anomaly detection and unsupervised learning (1)

Data warehouse construction - DWT floor

管理区解耦架构见过吗?能帮客户搞定大难题的

云函数实现网站自动化签到配置详解【Web函数/Nodejs/cookie】

Download the latest version of visual studio code and connect to the server remotely (very detailed)

追踪伦敦银实时行情的方法有哪些?

SystemVerilog-连接和复制运算符

Techo hub Fuzhou Station dry goods attack | talk with developers about new industrial intelligence technology

B- 树 ~

selenium对接代理与seleniumwire访问开发者工具NetWork
随机推荐
Flask sends verification code in combination with Ronglian cloud
Requestvideoframecallback() simple instance
Have you seen the management area decoupling architecture? Can help customers solve big problems
将Word中的表格以图片形式复制到微信发送
Api 接口优化的那些技巧
In the second round, 1000 okaleido tiger were sold out in one hour after logging in to binance NFT again
【树莓派】widows电脑如何与树莓派连接
Depth first search (DFS) and its matlab code
CUDA related
JWT token related configuration (global configuration identity authentication rewrites authenticate method)
iNFTnews | 元宇宙购物体验将成为吸引消费者的一大利器
AQS principle
Error reporting: when the browser clicks the modify add button, there is no response and no error reporting. Solution
Copu Professor Lu Shouqun was invited to give a keynote speech at the open atom global open source summit
【commons-lang3专题】004- NumberUtils 专题
Several methods of multi-threaded sequential operation can be asked casually in the interview
[develop low code platform] low code rendering
[untitled]
Data warehouse construction - ads floor
DRF - web development mode, API interface, API interface testing tool, restful specification, serialization and deserialization, DRF installation and use