当前位置:网站首页>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
边栏推荐
- PowerCL 批量创建及管理虚拟交换机
- Genomic DNA isolation Worthington ribonuclease A
- curl (7) Failed connect to localhost8080; Connection refused
- 【C】 Drink soda and find a single dog
- Control fillet stroke materialshapedrawable
- VMware VCSA 7.0 Install
- Install MySQL using Yum for Linux
- How NAT configures address translation
- Summary of wrong questions of software designers
- Leetcode59. Spiral matrix II
猜你喜欢

Doip test development practice

Principle of meter skipping

VMware VCSA 7.0 Install

Detailed explanation of 9 common reasons for MySQL index failure

With the help of rpa+lcap, the enterprise treasurer management can be upgraded digitally

VMware VCSA 7.0 Install

Interpretation of ISO 13400 (doip) standard

【C】逆序字符串(俩种递归思路)

Centos7 install mysql8

Virtual lab basic experiment tutorial -8. Fourier transform (1)
随机推荐
Leetcode59. 螺旋矩阵 II
【C】逆序字符串(俩种递归思路)
ISO 13400(DoIP)标准解读
Servlet运行原理_API详解_请求响应构造进阶之路(Servlet_2)
leetcode 763. Partition Labels 划分字母区间(中等)
【小程序项目开发 -- 京东商城】uni-app 商品分类页面(上)
Briefly introduce the working principle and characteristics of block cipher encryption block link mode (cryptography shift cipher encryption and decryption)
【C】 Drink soda and find a single dog
JS advanced ES6 ~ es13 new features
Data warehouse: Doris' application practice in meituan
curl (7) Failed connect to localhost8080; Connection refused
Js判断数据类型的4种⽅式
Oracle创建表空间和用户
Worthington RNA determination detailed introduction
基于 FPGA 实现数字时钟详细原理讲解及验证结果
Word中的\n是什么?:^p
Multi sensor fusion positioning (I) -- 3D laser odometer
Solution: direct local.Aar file dependencies are not supported when building an aar
Leetcode62. 不同路径
1-4 类的复习