当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢
Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)
图解网络:什么是虚拟路由器冗余协议 VRRP?
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
Win10 can't be installed in many ways Problems with NET3.5
[AUTOSAR XIII NVM]
The arm core board / development board of Feiling equipped with Ti am62x made its debut in embedded world 2022
Rust ownership (very important)
Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
[AUTOSAR five methodology]
随机推荐
[AUTOSAR 11 communication related mechanism]
飞凌搭载TI AM62x的ARM核心板/开发板首发上市,亮相Embedded World 2022
【AutoSAR 三 RTE概述】
AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight
Thank you for being together for these extraordinary two years!
[shutter] image component (cached_network_image network image caching plug-in)
MySQL multi table joint deletion
链表中的节点每k个一组翻转
What is needed to develop a domestic arm intelligent edge computing gateway
Several cases of recursive processing organization
excel IF公式判断两列是否相同
【案例分享】让新时代教育发展与“数”俱进
Illustrated network: what is virtual router redundancy protocol VRRP?
【AutoSAR 十三 NVM】
详解RDD基本概念、RDD五大属性
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
Reading and writing speed of Reza rz/g2l arm development board storage and network measurement
mysql 多表联合删除
[AUTOSAR I overview]
leetcode-1964:找出到每个位置为止最长的有效障碍赛跑路线