当前位置:网站首页>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;
}
边栏推荐
- 场景之分页查询设计
- [laser principle and application -3]: foreign brands of lasers
- 【RTOS训练营】晚课学员问题
- Implementation process of adding loading effect to easycvr page
- JDBC implements the addition, deletion, modification and query of MySQL 8.0 database
- Analysis and practice of parameter parser handlermethodargumentresolver
- "Introduction to natural language processing practice" deep learning foundation - attention mechanism, transformer deep analysis and learning material summary
- 使用 SAP UI5 FileUploader 控件上传本地文件试读版
- User defined variables and extracted public variables of JMeter
- Test the concept of left shift and right shift
猜你喜欢

What is the difference between request forwarding and request redirection?

【RTOS训练营】GPIO知识和预习安排 + 晚课提问

Oauth2 and JWT

JDBC implements the addition, deletion, modification and query of MySQL 8.0 database

【秒杀概念】大小端

Spine_附件皮肤

Nanjie's embarrassment

【RTOS训练营】环形缓冲区、AT指令、预习安排和晚课提问

旅行+战略加速落地 捷途新产品矩阵曝光

HCIA comprehensive experiment
随机推荐
If the native family is general, and the school is also a college on the rotten street, how to go on the next journey
中心对称的二进制模式CSLBP,matlab
[RTOS training camp] about classes and Q & A
【RTOS训练营】继续程序框架、tick中断补充、预习、课后作业和晚课提问
场景之分页查询设计
Cf1494f delete the edges (Euler circuit)
109. 使用 SAP UI5 FileUploader 控件上传本地文件
[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] course learning methods and structural knowledge review + linked list knowledge
《自然语言处理实战入门》深度学习基础 ---- attention 注意力机制 ,Transformer 深度解析与学习材料汇总
[RTOS training camp] problems of evening students
[RTOS training camp] operation explanation, queue and ring buffer, queue - transmission data, queue - synchronization tasks and evening class questions
腾讯员工晒出薪资:真实985毕业薪资,大家看我还有救吗?网友:日薪?
【RTOS训练营】程序框架、预习、课后作业和晚课提问
[RTOS training camp] program framework, preview, after-school homework and evening class questions
Four common simple and effective methods for changing IP addresses
Cnosdb Nirvana Rebirth: abandon go and fully embrace rust
JDBC implements the addition, deletion, modification and query of MySQL 8.0 database
MySQL pymysql operation
力扣 25. K 个一组翻转链表