当前位置:网站首页>Force buckle 25. Turn over the linked list in a group of K
Force buckle 25. Turn over the linked list in a group of K
2022-07-26 01:02:00 【Three watch ghost】
Title source :https://leetcode.cn/problems/reverse-nodes-in-k-group/
General meaning :
Give a linked list and an integer k, Start the list from scratch every k Flip once , If the number of the last paragraph is insufficient k individual , There is no need to flip
Ideas
- Design a function to flip the linked list , Given nodes node And length k, Can be node At the beginning k Nodes flipped . Attention should be paid to when designing , After flipping node.next You need to point to the last node of the linked list before flipping next
- Use a variable to save the start node of the current linked list segment to be flipped , Traverse the original list , Every traversal k Time , Flip , Attention should be paid after turning , Of the previous node of the first node of the current linked list segment next Need to point to The first node after flipping
See code for details
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null || k == 1) {
return head;
}
// Number of nodes currently traversed
int count = 0;
// The start node of the currently flipped linked list segment
ListNode newHead = head;
// The node currently traversed
ListNode cur = head;
// sentry
ListNode ans = new ListNode(-1);
// The last section flips the tail node of the linked list
ListNode lastTail = ans;
while (cur != null) {
// Update the number of nodes traversed
count++;
// If the number of traversal nodes is equal to k, Flip
if (count == k) {
// First save the original header node of the current segment , That is, the inverted tail node
ListNode newTail = newHead;
// Reverse a linked list , At the same time, save the inverted head node of the next segment
newHead = reverse(newHead, k);
// Of the tail node of the previous paragraph next To point to the inverted head node
lastTail.next = newHead;
// Reset count
count = 0;
// Update the tail node of the previous paragraph
lastTail = newTail;
// Update the first node before turning the next segment
newHead = newTail.next;
// Update the current traversal location
cur = lastTail;
}
cur = cur.next;
}
return ans.next;
}
/** * From the given head Node start k Nodes flipped * @param head * @param k * @return */
public ListNode reverse(ListNode head, int k) {
// The length of the current flip
int count = 1;
// Last convenient node
ListNode last = head;
// Currently traversing
ListNode cur = head.next;
// Temporary variable
ListNode temp;
// If the flipped length is not equal to k when , Continue flipping
while (count != k) {
// The length of the current flip +1
count++;
// Save the next node to traverse
temp = cur.next;
// Flip
// Point the current linked list pointer to the previous node
cur.next = last;
// Update the last traversal node
last = cur;
// Update the next traversal node
cur = temp;
}
// newTail -> oldTail.next
// head It is the... After turning k Nodes , its next Point to the original k Of nodes next, That is the first. k + 1 Nodes
head.next = cur;
// Return to the first node after flipping
return last;
}
边栏推荐
- JDBC implements the addition, deletion, modification and query of MySQL 8.0 database
- 《自然语言处理实战入门》深度学习基础 ---- attention 注意力机制 ,Transformer 深度解析与学习材料汇总
- 数据写入excel并填充颜色
- [RTOS training camp] operation explanation, queue and ring buffer, queue - transmission data, queue - synchronization tasks and evening class questions
- MySQL pymysql operation
- Download exclusively | Alibaba cloud maxcompute questions and answers to unlock SaaS mode cloud data warehouse in this electronic manual!
- [RTOS training camp] equipment subsystem, questions of evening students
- [laser principle and application-4]: internal structure and working principle of laser
- The bumpy road of referencing jar package json-path.jar in jmeter/idea
- 【RTOS训练营】晚课学员问题
猜你喜欢
![[laser principle and application-4]: internal structure and working principle of laser](/img/42/963c151c15c6b633ec0cf633355fb5.jpg)
[laser principle and application-4]: internal structure and working principle of laser

Zabbix监控主机及资源告警

Suddenly found an optimization artifact

使用 SAP UI5 FileUploader 控件上传本地文件试读版

REST-assured接口测试框架详解

Open download! Alibaba Devops Practice Manual
![[install software after computer reset] software that can search all files of the computer, the best screenshot software in the world, free music player, JDK installation, MySQL installation, installa](/img/d6/45f17c50f13d0bae1d14b317f9ae30.png)
[install software after computer reset] software that can search all files of the computer, the best screenshot software in the world, free music player, JDK installation, MySQL installation, installa
![[RTOS training camp] task scheduling (Continued), task comity, scheduling summary, queue and evening class questions](/img/0f/ca576a2f90aba4ddbb1f8b3e8a8ced.jpg)
[RTOS training camp] task scheduling (Continued), task comity, scheduling summary, queue and evening class questions

Lock upgrade: no lock, bias lock, lightweight lock, heavyweight lock

【MATLAB appdesigner】27_ How to debug and view variables in appdesigner? (examples + skills)
随机推荐
The bumpy road of referencing jar package json-path.jar in jmeter/idea
109. Upload local files using SAP ui5 fileuploader control
[RTOS training camp] operation explanation, queue and ring buffer, queue - transmission data, queue - synchronization tasks and evening class questions
开放下载!《阿里巴巴 DevOps 实践手册》
Inverse matrix block matrix
微波炉整流二极管 CL01-12
android sqlite先分组后排序左连查询
Spine_附件皮肤
如何mysql只要重复数据?
力扣 25. K 个一组翻转链表
Data is written into excel and filled with color
ZABBIX monitoring host and resource alarm
我们没有退路了
matlab 移位操作基础
【RTOS训练营】GPIO知识和预习安排 + 晚课提问
Timeout settings for feign and hystrix
Implementation process of adding loading effect to easycvr page
【RTOS训练营】继续程序框架、tick中断补充、预习、课后作业和晚课提问
MMOCR使用指南
Upload local file trial version using SAP ui5 fileuploader control