当前位置:网站首页>Using recursion and chain header interpolation to realize the group turnover of linked lists -- leetcode25 K group turnover linked lists
Using recursion and chain header interpolation to realize the group turnover of linked lists -- leetcode25 K group turnover linked lists
2022-07-29 00:11:00 【dor.yang】
Topic content
Give you the head node of the list head , Every time k Group of nodes to flip , Please return to the modified linked list .
k Is a positive integer , Its value is less than or equal to the length of the linked list . If the total number of nodes is not k Integer multiple , Please keep the last remaining nodes in the original order .
You can't just change the values inside the node , It's about actually switching nodes .
Example 1:

Input :head = [1,2,3,4,5], k = 2
Output :[2,1,4,3,5]
Example 2:

Input :head = [1,2,3,4,5], k = 3
Output :[3,2,1,4,5]
Tips :
The number of nodes in the linked list is n
1 <= k <= n <= 5000
0 <= Node.val <= 1000
Advanced : You can design one that uses only O(1) Does the algorithm of extra memory space solve this problem ?
source : Power button (LeetCode)
link :https://leetcode.cn/problems/reverse-nodes-in-k-group
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
My solution
There are some similarities between the words of this question and the previous pairing Exchange , But the difference is that here we are k Flip them in groups .
For the flip of the linked list , I was really not very skilled at first , But after trying to draw a picture, I probably understood 
We need three pointers , Two are used to record the flipped head and tail , A node used to calibrate a new plug .
We also need a marker , Tell the system when the turnover of a group is completed .
After finishing a group , Later, we can simply repeat this operation , It is worth noting that ,head Because of the principle of continuous plug , It just points to the head of the next group of nodes , So you can recurse directly .
Last run , I didn't expect the effect to be very good .
Implementation code
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode* now=head;
ListNode* l=head;
for(int i=0;i<k;i++){
if(now==nullptr){
return head;
}
now=now->next;
}
// now=now->next;
if(k==1){
return head;
}
ListNode* biao=head->next;
while(biao!=now){
head->next=biao->next;
biao->next=l;
l=biao;
if(head->next==now){
break;
}
biao=head->next;
}
head->next=reverseKGroup(head->next,k);
return biao;
}
};
ps: The above is my explanation and code
边栏推荐
- 【C】 Introduction and Simulation Implementation of ATOI and offsetof
- PowerCL 批量创建及管理虚拟交换机
- PIP image download
- Servlet operation principle_ API details_ Advanced path of request response construction (servlet_2)
- 【C】喝汽水,找单身狗问题
- JS高级 之 ES6~ES13 新特性
- Oracle创建表空间和用户
- Leetcode 763. partition labels divide alphabetic intervals (medium)
- Leetcode59. 螺旋矩阵 II
- What do you need to bring with you for the NPDP exam? Stationery carrying instructions
猜你喜欢

Okaleido ecological core equity Oka, all in fusion mining mode

Leetcode62. Different paths

Application of Devops in Internet of things solutions

AutoCAD -- import excel tables into CAD and merge CAD

IDEA报错Error running ‘Application‘ Command line is too long解决方案

Multi sensor fusion positioning (I) -- 3D laser odometer

Leetcode63. Different paths II

EN 1935建筑五金.单轴铰链—CE认证

CANoe应用案例之DoIP通信

Detailed principle explanation and verification results of digital clock based on FPGA
随机推荐
Install MySQL using Yum for Linux
Tyrosine decarboxylase -- characteristics of tyrosine decarboxylase of Streptococcus faecalis in Worthington
Genomic DNA isolation Worthington ribonuclease A
PowerCL 批量创建及管理虚拟交换机
Servlet operation principle_ API details_ Advanced path of request response construction (servlet_2)
以JSP为视图解析器搭建SSM项目
EN 1873 assembly accessories for roofing - plastic single roof lamps - CE certification
Real time data warehouse: Didi's real-time data warehouse landing practice
Application of Devops in Internet of things solutions
DevOps在物联网解决方案中的应用
Real time data warehouse: Netease strictly selects the practice of real-time data warehouse based on Flink
实时数仓:美团点评Flink的实时数仓应用分享
实时数仓:滴滴的实时数仓落地实践
Interpretation of ISO 13400 (doip) standard
VS2005 accesses the setting method "recommended collection" of vss2005 through sourceoffsite
Leetcode62. 不同路径
Eight performance analysis indicators of effective supply chain management (Part 1)
1-4 类的复习
1-7 解决类中方法的this指向问题
Powercli batch add esxi to vCenter