当前位置:网站首页>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
边栏推荐
- Web系统常见安全漏洞介绍及解决方案-CSRF攻击
- 【C】 Reverse string (two recursive ideas)
- Have passed hcip and joined the company of your choice, and share the learning experience and experience of Huawei certification
- Android studio connects to MySQL and completes simple login and registration functions
- Install MySQL using Yum for Linux
- ZABBIX 5.0 uses its own redis template for monitoring
- Briefly introduce the working principle and characteristics of block cipher encryption block link mode (cryptography shift cipher encryption and decryption)
- 【小程序项目开发 -- 京东商城】uni-app 商品分类页面(上)
- Build SSM project with JSP as view parser
- Urease -- Characteristics and determination scheme of Worthington jack bean urease
猜你喜欢

Android studio连接MySQL并完成简单的登录注册功能

Intelligent trash can (VII) -- Introduction and use of sg90 steering gear (Pico implementation of raspberry pie)

Connection pool - return connection details (Part 2)

PowerCL 批量创建及管理虚拟交换机

Servlet运行原理_API详解_请求响应构造进阶之路(Servlet_2)

Develop effective Tao spell

Es6操作教程

Application of Devops in Internet of things solutions

失败率高达80%,数字化转型如何正确完成战略规划?

Powercli VMware vCenter deploys conventional new VMS in batch through self built PXE server with one click
随机推荐
Word中的\n是什么?:^p
Hutool official website (is hutool easy to use)
ISO 13400(DoIP)标准解读
Powercli batch add esxi to vCenter
Create AP hotspots for imx6 development board QT system based on rtl8723 cross compile iptables
Zibo station construction guide (aigler)
Urease -- Characteristics and determination scheme of Worthington jack bean urease
Dual for loop optimization
Applet editor rich text editing and rich text parsing
Intelligent trash can (VII) -- Introduction and use of sg90 steering gear (Pico implementation of raspberry pie)
Leetcode59. 螺旋矩阵 II
Field injection is not recommended solution
Exchange 2013 SSL certificate installation document
How NAT configures address translation
EN 1873屋面用装配附件.塑料单个屋面灯—CE认证
PowerCLi 批量添加esxi到vCenter
Oracle创建表空间和用户
The failure rate is as high as 80%. How to correctly complete the strategic planning of digital transformation?
【小程序项目开发 -- 京东商城】uni-app 商品分类页面(上)
Where is the DP interface of desktop computer (what if the host has no DP interface)