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
 
 
SOLUTION 1
用递归实现,逐层进行反转。遇到最后一个如果个数不为k,再反转一次即可。
 /*
SOLUTION 2: A better rec version.
*/
public ListNode reverseKGroup2(ListNode head, int k) {
if (head == null) {
return null;
} return rec2(head, k);
} public ListNode rec2(ListNode head, int k) {
if (head == null) {
return null;
} ListNode dummy = new ListNode(0); ListNode cur = head;
int cnt = 0;
while (cur != null) {
ListNode tmp = cur.next;
cur.next = dummy.next;
dummy.next = cur; cur = tmp; cnt++; // reverse a k group.
if (cnt == k) {
// BUG 1:
head.next = rec2(tmp, k);
return dummy.next;
}
} // we don't have k nodes.
if (cnt != k) {
cur = dummy.next;
dummy.next = null; // reverse again.
while (cur != null) {
ListNode tmp = cur.next;
cur.next = dummy.next;
dummy.next = cur; cur = tmp;
}
} return dummy.next;
}

SOLUTION 2

另一个思路的递归:

先查看有没有k个node,如果有,切开2个链表,反转当前链表,并且使用递归处理下一个section,最后再把2者连接起来即可。

 public ListNode reverseKGroup1(ListNode head, int k) {
if (head == null) {
return null;
} return rec(head, k);
} // Solution 1: Recursion.
public ListNode rec(ListNode head, int k) {
// Reverse k and link to the next section.
ListNode dummy = new ListNode(0);
dummy.next = head; // find the tail node of the section. If not find, just return.
int cnt = k;
ListNode tail = dummy;
while (cnt > 0 && tail != null) {
cnt--;
tail = tail.next;
} // We don't have k nodes to revers.
// bug 1: we should judge that if tail == null to avoid the overflow.
if (tail == null) {
return head;
} // cut the 2 list.
ListNode next = tail.next;
tail.next = null; // reverse the first list.
ListNode newHead = reverse(head); // reverse the next section.
next = rec(next, k); // link the 2 sections.
head.next = next; return newHead;
} public ListNode reverse(ListNode head) {
ListNode dummy = new ListNode(0);
while (head != null) {
ListNode tmp = head.next;
head.next = dummy.next;
dummy.next = head; head = tmp;
} return dummy.next;
}

SOLUTION 3

使用一个专用的反转函数来进行反转,从头到尾遍历,遍历到K的时候,使用Pre-Next指针的方式进行反转。这个方法比递归更棒。

要特别注意的是:

reverseSection 函数中,while 循环的终止条件不是cur != null,而是cur != next。这一点要特别注意,否则很容易造成死循环!

// BUG: Severe. if we use cur != null here, we will cause very serious loop error.
while (cur != next) {
   ...
}

 /*
SOLUTION 3: A Iteration version.
*/
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null) {
return null;
} ListNode dummy = new ListNode(0);
dummy.next = head; ListNode pre = dummy;
ListNode cur = pre.next; int cnt = 0;
while (cur != null) {
cnt++;
cur = cur.next; if (cnt == k) {
cnt = 0;
pre = reverseSection(pre, cur);
cur = pre.next;
}
} return dummy.next;
} /**
* 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 reverseSection(ListNode pre, ListNode next){
ListNode cur = pre.next; // record the new tail.
ListNode tail = cur; // BUG: Severe. if we use cur != null here, we will cause very serious loop error.
while (cur != next) {
ListNode tmp = cur.next;
cur.next = pre.next;
pre.next = cur;
cur = tmp;
} tail.next = next;
return tail;
}

GITHUB:

1. 主页君的GitHub代码

2. 2014.1227 Redo:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/list/ReverseKGroup_1227_2014.java

ref: http://www.cnblogs.com/lichen782/p/leetcode_Reverse_Nodes_in_kGroup.html

LeetCode: Reverse Nodes in k-Group 解题报告的更多相关文章

  1. [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 ...

  2. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  3. LeetCode: Reverse Words in a String 解题报告

    Reverse Words in a String Given an input string, reverse the string word by word. For example,Given ...

  4. 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 算法思想:基本操作就是链表 ...

  5. 【LeetCode】743. Network Delay Time 解题报告(Python)

    [LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...

  6. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  7. 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)

    [LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...

  8. 【LeetCode】732. My Calendar III解题报告

    [LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...

  9. 【LeetCode】764. Largest Plus Sign 解题报告(Python)

    [LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  10. 【LeetCode】851. Loud and Rich 解题报告(Python)

    [LeetCode]851. Loud and Rich 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

随机推荐

  1. Redis学习之路(000)- 目录

    本文是博主学习整理网上大神的文件以及自学的心得. Redis学习之路(000)- 目录 Redis学习之路(001)- Redis介绍以及安装(Linux) Redis学习之路(002)- Ubunt ...

  2. 懒得说IE6了,写个js插件不能写注释,原因如下

    变态的ie6将注释当代码解释 ie6宽松的安全环境对于开发人员是开心的,比如运行速度快(对于ie7/8/9).支持部份文件操作等.但也有很多烦忧,比如对数组.对象的检测比较机械,这还不算什么,这两天让 ...

  3. Easyui 中 Tabsr的常用方法

    注:index必须为变量 tab页从0开始 //新增一个tab页var index = 0;$('#tt').tabs('add',{ title: 'Tab'+index, content: '&l ...

  4. 在没有创建Provision Profile权限的情况下 发布Enterprise inhouse app 的方法

    由于用普通开发者证书,发布的app或ipa没法在普通机器上安装运行,原因是apple限制了普通开发者发布appstore以外的环境中,只有Enterprise企业版证书才能发布inhouse. 今天在 ...

  5. Effective C++ 34

    34.将文件间的编译依赖性降到最低. 对于一个大型程序,其结构是错综复杂的.当你对一个类进行一些改动时.改动的不是接口,而是类的实现,即仅仅是一些细节部分,但又一次生成程序时,所实用到这个类的的文件都 ...

  6. 如何在"Visual Studio Code"中使用" Git" 进行版本控制

    如何在"Visual Studio Code"中使用" Git" 进行版本控制 本来认为此类教程,肯定是满网飞了.今天首次使用VS Code的Git功能,翻遍了 ...

  7. mysql在插入或更新的时候对一个字段赋递增值

    在我们有一个需求是更新某个表里面的某个字段,让这个字段变成一个递增的值,或者插入某个表中使得某些字段的值是递增的.下面我们来看看具体的操作: 更改表字段为递增 首先设置一个变量,初始值为任意数值,这里 ...

  8. unity, shader中获得当前像素深度

    frag shader中直接访问i.pos.z就是深度,不必除以i.pos.w,因为系统已经自动进行过了透视除法且已将i.pos.w置为0.

  9. php基础面试题:

    1:print.print_r.echo到底有什么区别? 2:get和post的区别 echo和print都可以做输出,不同的是,echo不是函数,没有返回值,而print是一个函数有返回值,所以相对 ...

  10. Subversion代码提交中的org.apache.subversion.javahl.ClientException: svn: E200007: Commit failed异常解决

    : 在切换subversion服务器地址之后,发生的无法正确提交代码的问题org.apache.subversion.javahl.ClientException: svn: E200007: Com ...