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 ...
随机推荐
- mysql数据库外键删除更新规则
1.CASCADE:从父表删除或更新且自动删除或更新子表中匹配的行. 2.SET NULL:从父表删除或更新行,并设置子表中的外键列为NULL.如果使用该选项,必须保证子表列没有指定NOT NULL. ...
- Android 中点击返回键弹出“在按一次退出程序”的做法
在很多应用中都有这种做法,连续点击两次才退出程序,目的是为了防止用户不小心点击到了返回键而退出程序了,添加了一次用户确认的过程. 其实做法很简单,只需要做一个延时的消息处理就可以了. 首先在我们在一个 ...
- 【JavaScript代码实现一】数组去重
function arrayNoDupulate(array) { var hash = {}; var result = []; for(var i=0;i<array.length;i++) ...
- Codeforces Round #357 (Div. 2) E. Runaway to a Shadow 计算几何
E. Runaway to a Shadow 题目连接: http://www.codeforces.com/contest/681/problem/E Description Dima is liv ...
- C#高级编程9-第10章 集合
集合 1.集合接口和类型 接口 说明 IEnumerable<T> 如果foreach语句用于集合,就需要IEnumerable接口.这个借口定义了方法GetEnumerator(),他返 ...
- HDU 2222 Keywords Search 【AC自动机模板】
询问有多少个模式串出现在了文本串里面.将模式串插入Trie树中,然后跑一边AC自动机统计一下就哦了. 献上一份模板. #include <cstdio> #include <cstr ...
- vs2017与docker
基本需求 系统 win10 vs2017 docker 步骤 1.开启系统的hyper-v 2. 重启电脑 3.安装docker 下载地址:https://docs.docker.com/docker ...
- Phone重绘机制drawRect 转
Phone重绘机制drawRect 如何使用iPhone进行绘图.重绘操作iPhone的绘图操作是在UIView类的drawRect方法中完成的,所以如果我们要想在一个UIView中绘图,需要写一个扩 ...
- Arcgis Runtime for andriod 100 加载geodatabase
private void LoadMY(){ try { String mainGeodatabaseFilePath = YLPub.getMapData() + "/gismap/sl. ...
- LR杂记-nmon+analyser监控linux系统资源
1.查看linux具体版本号信息 file /sbin/init 2.下载相应nmon版本号 http://pkgs.repoforge.org/nmon/ 3.安装 rpm -ivh nmon-14 ...