当前位置:网站首页>[notes for question brushing] specified interval reversal in the linked list
[notes for question brushing] specified interval reversal in the linked list
2022-07-29 01:00:00 【Qihai】
subject :
describe
Set the number of a node to size Linked list m Position to n Interval reversal between positions , Time complexity required O(n)O(n), Spatial complexity O(1)O(1).
for example :
The list given is 1\to 2 \to 3 \to 4 \to 5 \to NULL1→2→3→4→5→NULL, m=2,n=4m=2,n=4,
return 1\to 4\to 3\to 2\to 5\to NULL1→4→3→2→5→NULL.
Data range : Chain length 0 < size \le 10000<size≤1000,0 < m \le n \le size0<m≤n≤size, The value of each node in the linked list meets |val| \le 1000∣val∣≤1000
requirement : Time complexity O(n)O(n) , Spatial complexity O(n)O(n)
Advanced : Time complexity O(n)O(n), Spatial complexity O(1)O(1)
Example 1
Input :
{1,2,3,4,5},2,4Return value :
{1,4,3,2,5}Example 2
Input :
{5},1,1Return value :
{5}Topic link : Reverse the specified interval in the linked list _ Niuke Tiba _ Cattle from (nowcoder.com)
answer :
As long as you master the reverse linked list, you can generally solve this problem , As for the given interval, you only need to traverse here .
Our goal is to reverse the entire list while traversing . So can we fix the initial node , Then every time you cycle , Just move the next fixed node to the front of the fixed node , Let the fixed node in front of the fixed node point to this mobile node , Then the mobile node points to the next fixed node at the beginning .( Point to the first node at the beginning , Then point to the second , This is equivalent to rearranging the front fixed node and the fixed node in reverse order ). In this way, you can move by how many steps .
The front fixed node is res, Fixed node is cur. In order to prevent the first row from the beginning , Then the front fixed node needs to create a head node , It's worth it , Then point to the head , In this way, when returning, you can return the head it points to .
Then through the cycle, that is i < m Find the first node ,res It is equal to the previous node , that cur Is to point to the latter .
Code :
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;
}边栏推荐
- Five interesting magic commands in jupyter notebook
- 华为发布HarmonyOS 3.0,向“万物互联”再迈一步
- 散列表 ~
- 状态压缩dp-蒙德里安的梦想
- Kwai focuses on regulating the number maintenance behavior in the ways of handling and manuscript washing, and how to purify the content ecology on the we media platform
- Inftnews | yuanuniverse shopping experience will become a powerful tool to attract consumers
- Xinchi technology released the latest flagship product of G9 series, equipped with six A55 cores with 1.8GHz dominant frequency
- Surfacecontrol and surfaceflinger communication
- ThinkPHP高仿蓝奏云网盘系统程序
- Irregular clipping of NC data with CDO
猜你喜欢

Wechat campus bathroom reservation of small program completion work (6) opening defense ppt

In the second round, 1000 okaleido tiger were sold out in one hour after logging in to binance NFT again

浅谈一下跨端技术方案

Outlier detection and Gan network (1)

AQS principle

QT静态编译程序(Mingw编译)

Charles -- 从0-1教你如何使用抓包工具

PLATO上线LAAS协议Elephant Swap,用户可借此获得溢价收益

【无标题】

消费行业数字化升级成“刚需”,weiit新零售SaaS为企业赋能!
随机推荐
Tips for API interface optimization
redis版本怎么查看(查看redis进程)
🧐 Table1 | finish your third line watch in one second
DRF - paging, JWT introduction and principle, JWT quick use, JWT source code analysis, JWT custom return format, custom user issued token, custom token authentication class
SurfaceControl和SurfaceFlinger通信
【无标题】
day8
Andriod6.0 low power mode (turn off WiFi, Bluetooth, GPS, screen brightness, etc.)
B+ tree~
[network security] complete the blacklist and whitelist functions of server firewall through iptables and ipset
Some considerations about ThreadPool
B站“崩溃”始末 2021.07.13 我们是这样崩的(转载)
【commons-lang3专题】004- NumberUtils 专题
Techo hub Fuzhou Station dry goods attack | talk with developers about new industrial intelligence technology
华为发布HarmonyOS 3.0,向“万物互联”再迈一步
散列表 ~
DRF - web development mode, API interface, API interface testing tool, restful specification, serialization and deserialization, DRF installation and use
双链表的定义 ~
Selenium wire obtains Baidu Index
主线程与守护线程