Java [leetcode 25]Reverse Nodes in k-Group
题目描述:
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.
Only constant memory is allowed.
For example,
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
解题思路:
仍然是采用递归的方法,跟上一题类似。按照k个数字为一段的方法递归判断。如果该段长度不够k,那么直接返回该段的值。否则将该段k个数字从第一个数字开始两个两个反转。
代码如下:
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null)
return null;
ListNode tmp = head;
for (int i = 1; i < k; i++) {
tmp = tmp.next;
if (tmp == null)
return head;
}
ListNode nextRoundHead = tmp.next;
ListNode firstNode = head;
ListNode secondNode = head.next;
ListNode thirdNode;
for (int i = 1; i < k; i++) {
thirdNode = secondNode.next;
secondNode.next = firstNode;
firstNode = secondNode;
secondNode = thirdNode;
}
head.next = reverseKGroup(nextRoundHead, k);
return tmp;
}
Java [leetcode 25]Reverse Nodes in k-Group的更多相关文章
- Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...
- LeetCode 25 Reverse Nodes in k-Group Add to List (划分list为k组)
题目链接: https://leetcode.com/problems/reverse-nodes-in-k-group/?tab=Description Problem :将一个有序list划分 ...
- [Leetcode] Reverse nodes in k group 每k个一组反转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- 蜗牛慢慢爬 LeetCode 25. Reverse Nodes in k-Group [Difficulty: Hard]
题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
- [LeetCode] 25. Reverse Nodes in k-Group 每k个一组翻转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- [leetcode]25. Reverse Nodes in k-Group每k个节点反转一下
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- [LeetCode]25. Reverse Nodes in k-Group k个一组翻转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- [leetcode 25]Reverse Nodes in k-Group
1 题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified li ...
- [LeetCode] 25. Reverse Nodes in k-Group ☆☆☆
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
随机推荐
- wpf 制作播放视频的屏保程序、而且能分屏显示
这个程序用到了WPF里 “visual_Brush”(主要是为了实现分屏显示) , “UserControl” ,这两个知识点: 在屏保状态下播放指定文件夹下的视频,而且能分屏显示: 把编译好的屏保 ...
- Speech Patterns (string)
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- ffmpeg yuv转h264
ffmpeg -s 176x144 -i container_qcif_176_144.yuv -b:v 7776k -r 25 -vcodec libx264 ds.h264
- 三、mysql运算符
取模有2种方法: 或 mod(,) 比较一个字段的值是不是null有两种方法:is null 或 <=> null 不能直接使用 where id = null;是不对的
- 【Entity Framework 7】 完全不一样的玩法
http://www.cnblogs.com/n-pei/p/4274907.html
- Qt编译postgreSQL驱动
安装postgreSQL,安装目录下的lib和bin添加到path 打开Qt安装目录,找到src\plugins\sqldrivers\psql编辑psql.pro,添加INCLUDEPATH += ...
- 开源YYKit-b
转自 ibireme的博客,大家可以去他博客看看,有很多开发过程的一些调研和评测. YYModel 类似 Mantle/JSONModel 的工具,性能比 Mantle 高一个数量级,有更好的容错性, ...
- HDU 3339 In Action 最短路+01背包
题目链接: 题目 In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Hibernate注解方法使用总结
1.类级别注解 @Entity 映射实体类 @Table 映射数句库表 @Entity(name="tableName") - 必须,注解将一个类声明为一个实体bea ...
- PHP 性能分析第三篇: 性能调优实战
注意:本文是我们的 PHP 性能分析系列的第三篇,点此阅读 PHP 性能分析第一篇: XHProf & XHGui 介绍 ,或 PHP 性能分析第二篇: 深入研究 XHGui. 在本系列的 ...