当前位置:网站首页>[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;
}
};
边栏推荐
- C language explanation series - comprehensive exercises, guessing numbers games
- RNN循环神经网络
- What is spark serialization for?
- 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
- 【Day_06 0423】不要二
- Convolutional neural network (II) - deep convolutional network: case study
- Qu Weihai, chairman and CEO of Xinyi interactive, adheres to mutual benefit and win-win results, and Qu Weihai promotes enterprise development
- 二叉树的前中后序遍历——本质(每个节点都是“根”节点)
- 【Day_04 0421】计算糖果
- Interview difficulties: difficulties in implementing distributed session, this is enough!
猜你喜欢

1.12 basis of Web Development

多目标检测

漫谈软件缺陷管理的实践

招标信息获取

【pytorch】图片增广

逆序打印链表

Convolutional neural network (II) - deep convolutional network: case study

Workflow activiti5.13 learning notes (I)

Viewing the technology stack of distributed system from the crash report of station B

Etcd database source code analysis - cluster membership changes log
随机推荐
Calling mode and execution sequence of JS
The time complexity of two recursive entries in a recursive function
Acquisition of bidding information
Viewing the technology stack of distributed system from the crash report of station B
漫谈软件缺陷管理的实践
Interpretation of TPS motion (cvpr2022) video generation paper
Understanding the mathematical essence of machine learning
Byte interview question - judge whether a tree is a balanced binary tree
1.12 basis of Web Development
Mysql45 talking about infrastructure: how is an SQL query executed?
Database SQL language practice
Oc/swift Technology Download File (breakpoint continuation AFN download file alamofire Download File native download) (source code)
Jz36 binary search tree and bidirectional linked list
H. Take the elevator greedy
卸载手机自带APP的操作步骤
Widget is everything, widget introduction
Kingbasees SQL language reference manual of Jincang database (6. Expression)
Can you make a JS to get the verification code?
The number of weeks of Oracle last year and this year, with the start time and end time
JS的调用方式与执行顺序