当前位置:网站首页>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;
}
};
边栏推荐
- Understanding and distinguishing of some noun concepts in adjustment / filtering
- 安全运营四要素之资产、脆弱性、威胁和事件
- Leetcode-871: minimum refueling times
- KingbaseES ALTER TABLE 中 USING 子句的用法
- Basic use of sringcloud & use of component Nacos
- 【AutoSAR 二 AppL概述】
- 飞凌搭载TI AM62x的ARM核心板/开发板首发上市,亮相Embedded World 2022
- 【AutoSAR 四 BSW概述】
- Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities
- lex && yacc && bison && flex 配置的问题
猜你喜欢

Thank you for being together for these extraordinary two years!

excel去除小数点后面的数据,将数字取整

(C语言)数据的存储

tail -f 、tail -F、tailf的区别
![[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions](/img/ea/ee1ad50a497478b9d080bb5e4bdfb5.png)
[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions

FPGA - 7 Series FPGA internal structure clocking -04- multi area clock

详解RDD基本概念、RDD五大属性

飞凌搭载TI AM62x的ARM核心板/开发板首发上市,亮相Embedded World 2022

Is there a free text to speech tool to help recommend?

【AutoSAR 四 BSW概述】
随机推荐
[introduction to AUTOSAR seven tool chain]
Overlay of shutter (Pop-Up)
In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
详解RDD基本概念、RDD五大属性
链表内指定区间反转
Understanding and distinguishing of some noun concepts in adjustment / filtering
Machine learning: numpy version linear regression predicts Boston house prices
【AutoSAR 七 工具链简介】
Liad: the consumer end of micro LED products is first targeted at TVs above 100 inches. At this stage, it is still difficult to enter a smaller size
Several cases of recursive processing organization
Win10 can't be installed in many ways Problems with NET3.5
【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议
AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight
文件操作IO-Part2
cordova-plugin-device获取设备信息插件导致华为审核不通过
Test shift right: Elk practice of online quality monitoring
拥抱平台化交付的安全理念
RK3568开发板评测篇(二):开发环境搭建
数学建模之线性规划(含MATLAB代码)
RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide