当前位置:网站首页>[specified interval inversion in BM2 linked list]
[specified interval inversion in BM2 linked list]
2022-07-26 06:12:00 【On the Bank of Anhe Bridge】
Reverse the specified interval in the linked list
Title source
Cattle from : Reverse the specified interval in the linked list
Title Description
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→2→3→4→5→NULL,m=2,n=4,
return 1→4→3→2→5→NULL.
Data range : Chain length 0 <size≤1000,0 < m≤n≤size, The value of each node in the linked list meets ∣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,4
Return value
{1,4,3,2,5}
Example 2
Input
{5},1,1
Output
{5}
Thought analysis
- Define a virtual head node , Prevent the head node of the linked list from being inverted
- Find the starting position of the interval to be reversed and its precursor node
- Reverse from the starting position with the method of reverse of single linked list
- Splice the inverted part with the original linked list
- Returns the next node of the virtual head node
Code display
ListNode* reverseBetween(ListNode* head, int m, int n) {
if (head == NULL)
{
return NULL;
}
// The head node may also be reversed , Define a virtual head node
ListNode* myhead = new ListNode(0);// Apply for a new node
myhead->next = head;// The new node is connected with the original head node
ListNode* cur = myhead;
for (int i = 0; i < m - 1; i++)
{
cur = cur->next;
}
ListNode* prev = cur;// Start reversing the previous node of the point
ListNode* start = prev->next;// The node that begins to reverse
cur = start->next;
ListNode* before = start;
ListNode* next = nullptr;
while (m < n)
{
next = cur->next;
cur->next = before;
before = cur;
cur = next;
m++;
}
prev->next = before;//prev Of next Point to the starting point after inversion
start->next = cur;//start Become after reversing end, therefore start Of next Point to the next node at the end of the original
return myhead->next;
}
};
边栏推荐
- K. Link with Bracket Sequence I dp
- Mysql45 talking about global lock, table lock and row lock
- 数据库sql语言实战
- Should we test the Dao layer?
- Traversal of the first, middle, and last order of a binary tree -- Essence (each node is a "root" node)
- [SQL optimization] (big table tips) sometimes a 2-hour SQL operation may take only 1 minute
- Leetcode:934. The shortest Bridge
- Leetcode 347. top k high frequency elements
- L. Link with Level Editor I dp
- 分布式 | 实战:将业务从 MyCAT 平滑迁移到 dble
猜你喜欢

基于消防GIS系统的智慧消防应用
C language explanation series - comprehensive exercises, guessing numbers games

Jz36 binary search tree and bidirectional linked list

Talking about the practice of software defect management

Etcd database source code analysis - cluster membership changes log

招标信息获取

Docking wechat payment (II) unified order API

VRRP protocol and experimental configuration

Solutions to the failure of copy and paste shortcut keys

1.12 basis of Web Development
随机推荐
光量子里程碑:6分钟内解决3854个变量问题
Taobao pinduoduo Tiktok 1688 Suning taote jd.com and other keyword search commodity API interfaces (keyword search commodity API interface, keyword search commodity list interface, classification ID s
Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4
2022 National latest fire-fighting facility operator (Senior fire-fighting facility operator) simulation test questions and answers
【Day_02 0419】倒置字符串
Embedded sharing collection 15
Leetcode 42. rainwater connection
Detailed explanation of the whole process of coding's pressure measurement operation
Learn about spark project on nebulagraph
机械制造企业如何借助ERP系统,做好生产管理?
【Day04_0421】C语言选择题
Excitation method and excitation voltage of hand-held vibrating wire vh501tc acquisition instrument
ament_ Cmake generates the ros2 library and links it
Talking about the practice of software defect management
Taobao JD pinduoduo Tiktok taote 1688 and other multi platform commodity app details API interfaces (commodity details page data interface, commodity sales interface, keyword search commodity sales in
Flex layout
Kingbasees SQL language reference manual of Jincang database (11. SQL statement: abort to alter index)
Byte interview question - judge whether a tree is a balanced binary tree
H. Take the elevator greedy
Mysql45 talking about logging system: how does an SQL UPDATE statement execute?