当前位置:网站首页>Every k nodes in the linked list are flipped
Every k nodes in the linked list are flipped
2022-07-03 01:01:00 【Schuyler Hu】
problem
Every node in the given linked list k Turn over in groups , Returns the flipped linked list
If the number of nodes in the linked list is not k Multiple , Leave the last remaining nodes as they are
You cannot change the value in a node , Only the node itself can be changed .
Ideas
The double pointer determines the inversion interval , Then reverse the list , Return to the new header node , After the original head node is reversed, it becomes the tail node . Every recursive call , take k Nodes reversed , And return the inverted new head node .
Code implementation
/** * struct ListNode { * int val; * struct ListNode *next; * }; */
class Solution {
public:
/** * * @param head ListNode class * @param k int integer * @return ListNode class */
ListNode* reverseKGroup(ListNode* head, int k) {
// write code here
ListNode* tail = head;
// Walk forward k Step , Determine the inversion interval
for (int i = 0; i < k; i++)
{
if (!tail) return head;
tail = tail->next;
}
// Interval reversal , Return to the new header node
ListNode* newHead = reverse(head, tail);
// After reversing head become Tail node
head->next = reverseKGroup(tail, k);
return newHead;
}
ListNode* reverse(ListNode* head, ListNode* tail)
{
ListNode* pre = NULL;
ListNode* cur = head;
// List reversal , return tail Previous node
while (cur != tail)
{
ListNode* next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
return pre;
}
};
边栏推荐
- Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
- Usage of using clause in kingbases alter table
- 【AutoSAR 九 C/S原理架构】
- 指针进阶(一)
- [AUTOSAR five methodology]
- 瑞萨电子RZ/G2L开发板上手评测
- 【AutoSAR 六 描述文件】
- Leetcode-1964: find the longest effective obstacle race route to each position
- matlab查找某一行或者某一列在矩阵中的位置
- cordova-plugin-device获取设备信息插件导致华为审核不通过
猜你喜欢

【AutoSAR 七 工具链简介】

Reading and writing speed of Reza rz/g2l arm development board storage and network measurement
![[AUTOSAR II appl overview]](/img/da/76ccc05e2199705b20d8304bfb86b2.png)
[AUTOSAR II appl overview]

【AutoSAR 八 OS】
![[overview of AUTOSAR three RTE]](/img/6a/0df33beb42f165af77a17b5d8b01e2.png)
[overview of AUTOSAR three RTE]

【AutoSAR 三 RTE概述】

Teach you JDBC hand in hand -- structure separation

【AutoSAR 十一 通信相关机制】

指针进阶(一)

matlab将数字矩阵保存为地理空间数据出错,显示下标索引必须为正整数类型或逻辑类型,解决
随机推荐
KingbaseES ALTER TABLE 中 USING 子句的用法
465. 最优账单平衡 DFS 回溯
Win10 can't be installed in many ways Problems with NET3.5
数学建模之线性规划(含MATLAB代码)
Initial order of pointer (basic)
Test shift right: Elk practice of online quality monitoring
cordova-plugin-device获取设备信息插件导致华为审核不通过
The arm core board / development board of Feiling equipped with Ti am62x made its debut in embedded world 2022
In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
【AutoSAR 十 IO架构】
【C语言】分支和循环语句(上)
Leetcode-2115: find all the dishes that can be made from the given raw materials
[AUTOSAR XIII NVM]
Vulkan-实践第一弹
The difference between relational database and non relational database
指针进阶(一)
Thread start and priority
链表中的节点每k个一组翻转
Meaning of Tencent cloud free SSL certificate extension file
leetcode-871:最低加油次数