当前位置:网站首页>[leetcode] a group of K flipped linked lists
[leetcode] a group of K flipped linked lists
2022-06-11 01:49:00 【xiaoshijiu333】
#LeetCode A daily topic 【 Special topic of linked list 】
- K A set of flip list
https://leetcode-cn.com/problems/reverse-nodes-in-k-group/ - analysis
According to the given k value , Group and reverse the original linked list , The remaining deficiency k It's the same
My first idea is to find out the length of the linked list , Then calculate the number of groups according to the length , Then reverse each group , Finally, they can be spliced together - Realization
public ListNode reverseKGroup(ListNode head, int k) {
int length = 0;
ListNode temp = head;
while (temp != null) {
temp = temp.next;
length++;
}
int count = length / k;
ListNode ans = new ListNode(0);
ListNode node = ans, prev = null, next;
for (int i = 0; i < count; i++) {
// Record the first element before reversing the linked list , That is, the last element after inversion ( Commonly used for splicing )
temp = head;
for (int j = 0; j < k; j++) {
next = head.next;
head.next = prev;
prev = head;
head = next;
}
node.next = prev;
// node Point to temp, That is, let it point to the last node after inversion , Facilitate splicing
node = temp;
// Reset prev
prev = null;
}
node.next = head;
return ans.next;
}
LeetCode Time consuming :0ms
- Achieve two
Consolidate the recursive algorithm routine learned before , I also wrote a recursive algorithm :
Every time K A group of the original linked list is divided into , Continuous recursion , Until the linked list is empty or short k Recursion ends ;
Based on the current recursion , Reverse the current linked list to the result returned in the previous step of splicing - Recursive implementation
public ListNode reverseKGroup02(ListNode head, int k) {
if (head == null) {
return null;
}
ListNode node = head;
for (int i = 0; i < k - 1; i++) {
node = node.next;
// Insufficient length of linked list k
if (node == null) {
return head;
}
}
ListNode p = node.next;
node.next = null;
ListNode ans = reverseKGroup02(p, k);
// Reverse the standard template ,prev It is the result of inversion 、head Is the last value after inversion
ListNode prev = null, next, q = head;
while (q != null) {
next = q.next;
q.next = prev;
prev = q;
q = next;
}
head.next = ans;
return prev;
}
LeetCode Time consuming :0ms
- summary
This problem belongs to the deformation problem of reverse linked list , Can skillfully use the operation of reverse linked list should be very easy to solve ; For recursive methods , Or the trilogy mentioned before : Find end condition , Find the return value , What recursion can get and what it should do
边栏推荐
- Yanrong looks at how to realize the optimal storage solution of data Lake in a hybrid cloud environment
- Threejs: streamer effect encapsulation
- kubernetes 二进制安装(v1.20.15)(七)加塞一个工作节点
- Summary of SAS final review knowledge points (notes on Application of multivariate statistics experiment)
- 2021-2-26 compilation of programming language knowledge points
- Exj shaped children will be satisfied with mbtxe due to disconnection
- [cloud native | kubernetes] actual combat of ingress case
- There is a problem with numpy after CONDA installs pytoch
- QGC ground station tutorial
- Middleware_ Redis_ 05_ Persistence of redis
猜你喜欢

Detailed explanation of classic papers on OCR character recognition

QGC地面站使用教程

How to download web photos

关于概率统计中的排列组合

Yunna provincial administrative unit fixed assets management system

逻辑漏洞 / 业务漏洞

Px4 from abandonment to mastery (twenty four): customized model
![[leetcode] reverse linked list II](/img/03/5e4921a8fe50b3489b4b99310d1afb.jpg)
[leetcode] reverse linked list II

2021-2-14 gephi学习笔记

1.3 ROS 无人机简介
随机推荐
關於概率統計中的排列組合
Loki learning summary (1) -- the best choice of Loki small and medium-sized project log system
Hao expresses his opinions: what small good habits have you adhered to?
逻辑漏洞 / 业务漏洞
China-open-ssl编译的一些记录
MeterSphere教程:接口返回结果为空时如何进行断言
2021-02-27image processing of MATLAB
Dialog AlertDialog 、SimpleDialog、showModalBottomSheet、showToast Flutter 自定义 Dialog
Is the SQL query result different from what you expected? Mostly "null" is making trouble
2021-02-03 learning notes of MATLAB before the US games (grey prediction and linear programming)
Yunna Qingyuan fixed assets management and barcode inventory system
并发编程基础底层原理学习(四)
2.2. Ros+px4 simulation multi-point cruise flight - Square
Derivation of Kalman filter (KF) and extended Kalman filter (EKF)
Brief description of custom annotations
ros缺包怎么办
Classic questions: 01 backpack, complete backpack, multiple backpack, two-dimensional cost Backpack
How to download web photos
QGC地面站使用教程
[mavros] mavros startup Guide