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.
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
这个题目花了蛮长时间,主要是没搞清楚逆转一个链表的关系。弄了半天。其实是蛮简单和经典的题目。
解法一:比较挫的解法。要判断很多分支,所以容易出错。先放在这里当反面教材。
public static ListNode reverseKGroup(ListNode head, int k) {
// Start typing your Java solution below
// DO NOT write main() function
ListNode firstGHead = head;
int idx = 0;
ListNode pp = null;
while(head != null) {
ListNode c = head;
if(k <= 1 || c == null)return firstGHead;
for(int i = 0; i < k - 1&& c != null;i++) {
c = c.next;
}
if(c == null) break;
c = head;//save original head
int i = k - 1;
ListNode p = head;
ListNode pn = p.next;
ListNode lastEnd = pp;
while(i > 0){
p = head;
head = head.next;
pn = p.next;
pp = lastEnd;
int swap = 0;
while(swap < i){
p.next = pn.next;
pn.next = p;
if(pp != null)
pp.next = pn;
pp = pn;
pn= p.next;
swap++;
}
i--;
}
if(idx++ == 0) firstGHead = head;
head = c.next;
pp = c;
}
return firstGHead;
逆转那部分惨不忍睹啊。。。虽然过了leetcode,但是很不simple。还有,如果写成递归leetcode是不让你过的。
解法二:比较好的解法,又是leetcode讨论组的人做的。首先,搞清楚怎么逆转一个单链表。其实O(n)就可以了。第一个肯定是last one。然后我们每遍历到一个node,就把它放到最链表的首位,最后一个么,最后就成为第一个了。下面是一个简单逆转链表的程序。
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode pre = dummy;
ListNode cur = head.next;
ListNode last = head;
while(cur != null){
last.next = cur.next;
cur.next = pre.next;
pre.next = cur;
cur = last.next;
}
head = dummy.next;
reverse a linked list with a head node
因为有“放到链表首位”的操作,我们需要一个dummy的头节点,遇到的新节点我们simply state: pre.next = cur; 保持一个invariant就是last节点始终在最后(cur的前面一个)
然后我们有如下方法:
/**
* Reverse a link list between pre and next exclusively
* an example:
* a linked list:
* 0->1->2->3->4->5->6
* | |
* pre next
* 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;
}
reverse range
就是区间的reverse。因为题目要求的是k group逆转嘛。注意人返回的是最后一个(last)节点,这样下一个k-group就可以用上了。牛人的想法真是周到体贴~~。主方法里面,遍历的过程中每次都计数,每次到达k个节点,就可以使用pre和head.next调用上面的方法逆转了。给跪了。
public static ListNode reverseKGroup2(ListNode head, int k) {
if(head == null || k == 1) return head;
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode pre = dummy;
int i = 0;
while(head != null){
i++;
if(i % k ==0){
pre = reverse(pre, head.next);
head = pre.next;
}else {
head = head.next;
}
}
return dummy.next;
}
reverseKGroup
通过这道题,我们学会了:
有效的算法是简洁的!简洁的!!简洁的!!!
LeetCode 笔记系列六 Reverse Nodes in k-Group [学习如何逆转一个单链表]的更多相关文章
- [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 算法思想:基本操作就是链表 ...
- leetcode第24题--Reverse Nodes in k-Group
problem: Given a linked list, reverse the nodes of a linked list k at a time and return its modified ...
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
sql server 关于表中只增标识问题 由于我们系统时间用的过长,数据量大,设计是采用自增ID 我们插入数据的时候把ID也写进去,我们可以采用 关闭和开启自增标识 没有关闭的时候 ,提示一下错 ...
- 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. ...
- LeetCode 笔记系列 18 Maximal Rectangle [学以致用]
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...
- LeetCode 笔记系列16.3 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...
- LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]
题目:Given n non-negative integers representing an elevation map where the width of each bar is 1, com ...
- LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]
题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
随机推荐
- 【特别推荐】几款极好的 JavaScript 下拉列表插件
表单元素让人爱恨交加.作为网页最重要的组成部分,表单几乎无处不在,从简单的邮件订阅.登陆注册到复杂的需要多页填写的信息提交功能,表单都让开发者花费了大量的时间和精力去处理,以期实现好用又漂亮的表单功能 ...
- 使用XmlHelper添加节点C#代码
接着上一篇:http://keleyi.com/a/bjac/ttssua0f.htm在前篇文章中,给出了C# XML文件操作类XmlHelper的代码,以及使用该类的一个例子,即使用XmlHelpe ...
- TortoiseSVN使用教程
一.下载安装文件: 图1.1 安装顺序:1.VisualSVN-Server-2.6.5.msi:2.TortoiseSVN-1.8.2.24708-win32-svn-1.8.3.msi:3.Lan ...
- firefox中flash经常崩溃
建议: 1.安装flashblck插件 2.添加配置文件 在C:\Windows\SysWOW64\Macromed\Flash添加mmc.cfg. mmc.cfg的内容: SlientAutoUpd ...
- 将自己写的库上传到cocoapods(2015)
2015年以前上传到cocoapods的方式相较于现在比较麻烦,现在用不上在此也就不提了.现在上传到cocoapods只需要简单的几步即可. 1.首先你需要有一个自我感觉写的差不多的库. 2.注册tr ...
- iOS里常见的几种信息编码、加密方法简单总结
一.MD5 MD5编码是最常用的编码方法之一,是从一段字符串中通过相应特征生成一段32位的数字字母混合码. MD5主要特点是 不可逆,相同数据的MD5值肯定一样,不同数据的MD5值不一样(也不是绝对的 ...
- ‘Cordova/CDVPlugin.h’ file not found
phonegap项目,平时真机调试没什么问题.然后想打包成ipa了,去Product --> Archive 一下,然后就报错了,说:‘Cordova/CDVPlugin.h’ file not ...
- Scrum三大角色特点
灵感来自于一段冷笑话: 一天,一头猪和一只鸡在路上散步,鸡看了一下猪说,“嗨,我们合伙开一家餐馆怎么样?”,猪回头看了一下鸡说,“好主意,那你准备给餐馆起什么名字呢?”,鸡想了想说“餐馆名字叫火腿和鸡 ...
- NSNotificationCenter应用总结
通常我们在 iOS 中发生什么事件时该做什么是由 Delegate 实现的,例如 View 加载完后会触发 viewDidLoad. Apple 还为我们提供了另一种通知响应方式,那就是 NSNot ...
- 【C语言】外部函数和内部函数
目录 [外部函数] [内部函数] 1.外部函数 定义的函数能被本文件和其它文件访问(默认). 注:不允许有同名的外部函数. 2.内部函数 定义的函数只能被本文件访问,其它文件不能访问. 注:允许 ...