25. Reverse Nodes in k-Group
来源:互联网 发布:网络对学生的利与弊 编辑:程序博客网 时间:2024/05/21 06:14
题意: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
分析:同swapPairs
AC代码:
ListNode* reverseKGroup(ListNode* head, int k) { ListNode **pp = &head, *sub_head, *sub_del, *sub_pre, *sub_next; if(k == 1) return head; while((*pp) && ((*pp)->next) ){ sub_head = *pp; sub_pre = sub_head; sub_del = (*pp)->next; ListNode *test = *pp; int j; for(j = 0; j < k && test; ++j){ test = test->next; } if(j != k){ return head; } for(int i = 1; i < k && sub_head && sub_del; ++i){ sub_next = sub_del->next; sub_pre->next = sub_next; // delete *sub_del sub_del->next = sub_head; sub_head = sub_del; sub_del = sub_next; } *pp = sub_head; pp = &(sub_pre->next); } return head;}
0 0
- [LeetCode]25.Reverse Nodes in k-Group
- LeetCode --- 25. Reverse Nodes in k-Group
- LeetCode 25.Reverse Nodes in k-Group
- [Leetcode] 25. Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- 【LeetCode】25.Reverse Nodes in k-Group
- [leetcode] 25.Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- Leetcode-25.Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- leetcode 25. Reverse Nodes in k-Group
- leetcode 25. Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- 配置持续集成QuickBuild调用Ranorex进行安卓自动化测试
- 几行代码清空Xcode模拟器缓存 (附带 源码)
- activity左右滑动
- spark core源码分析3 Master HA
- LeetCode 328 odds and even numbers
- 25. Reverse Nodes in k-Group
- delphi要控制EXCEL时用的,不错,转了。
- 突击Mercurial SCM(HG)13---回退到指定版本
- 做为一个PHP的工作者第一篇blog
- 【MySQL存储引擎】MyISAM、InnoDB和MEMORY特性对比
- 【官方说明】Unity 5 引擎专业版和个人版解析
- Spring 依赖注入后行为实现
- [Leetcode]Merge Intervals
- Web网络相关_如何实现实时更新?