当前位置:网站首页>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;
}
};
边栏推荐
- 指针进阶(一)
- 图解网络:什么是虚拟路由器冗余协议 VRRP?
- 解决ReactNative使用webView存在缓存问题
- Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
- 安全运营四要素之资产、脆弱性、威胁和事件
- 12_微信小程序之微信视频号滚动自动播放视频效果实现
- Usage of using clause in kingbases alter table
- Infrared thermography temperature detection system based on arm rk3568
- 鏈錶內指定區間反轉
- Understanding and distinguishing of some noun concepts in adjustment / filtering
猜你喜欢
Thank you for being together for these extraordinary two years!
【AutoSAR 四 BSW概述】
【AutoSAR 六 描述文件】
RK3568开发板评测篇(二):开发环境搭建
深度剖析数据在内存中的存储
excel IF公式判断两列是否相同
RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
Key detection and sinusoidal signal output developed by Arduino
【爱死机】《吉巴罗》被忽略的细节
【AutoSAR 十三 NVM】
随机推荐
1038 Recover the Smallest Number
How to systematically learn machine learning
Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
Leetcode-2115: find all the dishes that can be made from the given raw materials
Explain the basic concepts and five attributes of RDD in detail
Key detection and sinusoidal signal output developed by Arduino
Test shift right: Elk practice of online quality monitoring
Leetcode-871: minimum refueling times
数组与集合性能比较
leetcode-871:最低加油次数
FPGA - 7 Series FPGA internal structure clocking -04- multi area clock
Vulkan-性能及精细化
【AutoSAR 十一 通信相关机制】
excel IF公式判断两列是否相同
Lex & yacc & bison & flex configuration problems
leetcode-1964:找出到每个位置为止最长的有效障碍赛跑路线
How to find out the currently running version of Solr- How do I find out version of currently running Solr?
Problèmes de configuration lex & yacc & Bison & Flex
Vulkan is not a "panacea"“
The arm core board / development board of Feiling equipped with Ti am62x made its debut in embedded world 2022