Reverse Nodes in k-Group leetcode java
题目:
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
题解:
这道题主要是利用reverse链表的方法,reverse的方法就是维护三个指针,然后别忘了保存next指针就行。
代码如下:
1 //http://www.cnblogs.com/lichen782/p/leetcode_Reverse_Nodes_in_kGroup.html
2 /**
3 * Reverse a link list between pre and next exclusively
4 * an example:
5 * a linked list:
6 * 0->1->2->3->4->5->6
7 * | |
8 * pre next
9 * after call pre = reverse(pre, next)
*
* 0->3->2->1->4->5->6
* | |
* pre next
* @param pre
* @param next
* @return the reversed list's last node, which is the precedence of parameter next
*/
private static ListNode reverse(ListNode pre, ListNode next){
ListNode last = pre.next;//where first will be doomed "last"
ListNode cur = last.next;
while(cur != next){
last.next = cur.next;
cur.next = pre.next;
pre.next = cur;
cur = last.next;
}
return last;
}
public static ListNode reverseKGroup(ListNode head, int k) {
if(head == null || k == 1)
return head;
ListNode dummy = new ListNode(0);
dummy.next = head;
int count = 0;
ListNode pre = dummy;
ListNode cur = head;
while(cur != null){
count ++;
ListNode next = cur.next;
if(count == k){
pre = reverse(pre, next);
count = 0;
}
cur = next;
}
return dummy.next;
}
Reference:http://codeganker.blogspot.com/2014/02/reverse-nodes-in-k-group-leetcode.html
Reverse Nodes in k-Group leetcode java的更多相关文章
- [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 ...
- Reverse Nodes In K Group,将链表每k个元素为一组进行反转---特例Swap Nodes in Pairs,成对儿反转
问题描述:1->2->3->4,假设k=2进行反转,得到2->1->4->3:k=3进行反转,得到3->2->1->4 算法思想:基本操作就是链表 ...
- Reverse Words in a String leetcode java
题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is ...
- 【Reverse Nodes in k-Group】cpp
题目: 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个结点进行链表反转(链表)
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 [学习如何逆转一个单链表]
题目:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
- LeetCode: Reverse Nodes in k-Group 解题报告
Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...
- Leetcode 25/24 - Reverse Nodes in k-Group
题目描述 Leetcode 24 题主要考察的链表的反转,而 25 题是 24 的拓展版,加上对递归的考察. 对题目做一下概述: 提供一个链表,给定一个正整数 k, 每 k 个节点一组进行翻转,最后返 ...
- [LintCode] 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 ...
随机推荐
- 详谈 Spring 中的 IOC 和 AOP
这篇文章主要讲 Spring 中的几个点,Spring 中的 IOC,AOP,下一篇说说 Spring 中的事务操作,注解和 XML 配置. Spring 简介 Spring 是一个开源的轻量级的企业 ...
- python opencv3 读写图像文件
git: https://github.com/linyi0604/Computer-Vision # coding:utf8 import cv2 # 读取一张图片, 第二个参数可选 image = ...
- Git 工具的使用,windows平台安装
先谈谈版本控制的一些事 如果你严肃对待编程,就必定会使用"版本控制系统"(Version Control System). 随着信息科技的发展,软件开发已不是小手工作坊,软件的规模 ...
- js异步任务处理方式
一.es6(es2015)之前:使用原始的callback函数,会陷入回掉地域 this.$http.jsonp('/login', (res) => { this.$http.jsonp('/ ...
- JDK篇
卸载系统自带的jdk 使用以下命令查看是否已经安装了jdk rpm -qa|grep java rpm -qa|grep jdk 如果已经安装了可能会得到下面的结果: java-1.4.2-gcj ...
- 最好的PHP博客系统
1.Wordpress http://www.wordpress.org/ B2基础上开发而来,这是国内用户比较喜欢而且用户较多的一个php博客程序,缘由是因为Wordpress提供大量插件和模板,让 ...
- 不用软件快速拥有几百个QQ群并都是管理员
不用软件快速拥有几百个QQ群并都是管理员!快速拥有有几十万精准数据库的方法 !和快速收集上亿邮箱的思维方法(附上5种赚钱方法).pdf_免费高速下载|百度云 网盘-分享无限制 http://pan.b ...
- 苹果Mac OS X系统十三年视觉变化发展史
1Mac OS 9 一个普通的桌面操作系统 经过多个测试版本后,苹果终于正式公布OS X 10.10 Yosemite操作系统.苹果称这个第11版的OS X系统是自从2001年问世以来在视觉效果上变化 ...
- powerdesigner 设置字段显示comment注释
在Columns标签下,一排按钮中找到这个按钮:Customize Columns and Filter
- Android调用手机中的应用市场,去评分的功能实现
在我们常常使用的软件当中,我们经常可以看到在软件的设置界面,有一个功能那就是去评分的功能,只要我们一点击“去评分”就会调用手机中的应用市场软件.一开始我以为这个功能的实现是要遍历整个手机中的软件包名, ...