问题:

以k个元素为一组,反转单向链表。比如:

输入: 1->2->3->4->5->6->7->8->null and k = 3

输出:3->2->1->6->5->4->8->7->null.

分析:

我们可以把整个链表分成多个长度为 k  的子链表, 然后,我们再反转每一个子链表(递归)。问题的关键是我们需要把每个子链表再连接起来。所以,对每一个子链表操作以后,我们需要返回该子链表的头(head),然后,我们设置前一个子链表的最后一个node,把它的next 设置成下一个链表返回的头(head),这样,所有的子链表就连接起来了。

  1. public static Node reverse (Node head, int k) {
  2. Node current = head;
  3. Node next = null;
  4. Node prev = null;
  5. int count = 0;
  6. /*reverse first k nodes of the linked list */
  7. while (current != null && count < k) {
  8. next  = current.next;
  9. current.next = prev;
  10. prev = current;
  11. current = next;
  12. count++;
  13. }
  14. /* next is now a pointer to (k+1)th node
  15. Recursively call for the list starting from current.
  16. And make rest of the list as next of first node */
  17. if(next !=  null) {
  18. head.next = reverse(next, k);
  19. }
  20. /* prev is new head of the input list */
  21. return prev;
  22. }
public static Node reverse (Node head, int k) {
Node current = head;
Node next = null;
Node prev = null;
int count = 0; /*reverse first k nodes of the linked list */
while (current != null && count < k) {
next = current.next;
current.next = prev;
prev = current;
current = next;
count++;
} /* next is now a pointer to (k+1)th node
Recursively call for the list starting from current.
And make rest of the list as next of first node */
if(next != null) {
head.next = reverse(next, k);
} /* prev is new head of the input list */
return prev;
}

这个问题也可以使用非递归的方法,基本上问题的处理方式和递归是一样的,但是,非递归的方式要稍微复杂一点,因为每次对子链表反转以后,我们需要更新前一个子链表最后一个node 的next 值。代码如下:

  1. class Node {
  2. int val;
  3. Node next;
  4. Node(int x) {
  5. val = x;
  6. next = null;
  7. }
  8. }
  9. public class Solution {
  10. public static void main(String[] args) {
  11. Solution s = new Solution();
  12. Node n1 = new Node(1);
  13. Node n2 = new Node(2);
  14. Node n3 = new Node(3);
  15. Node n4 = new Node(4);
  16. Node n5 = new Node(5);
  17. n1.next = n2;
  18. n2.next = n3;
  19. n3.next = n4;
  20. n4.next = n5;
  21. Node head = s.ReverseInGroups(n1, 2);
  22. while (head != null) {
  23. System.out.println(head.val);
  24. head = head.next;
  25. }
  26. }
  27. public Node ReverseInGroups(Node current, int k) {
  28. if (current == null || current.next == null ) return current;
  29. //store the new head of the list
  30. Node newHead = null;
  31. //store the last node in the sub-list,
  32. //we will update its next value when finishing
  33. //reversing the next sub-list
  34. Node previousGroupTail = null;
  35. int count = 1; //used to track the first sub-list
  36. while (current != null) {
  37. // the actual tail in the sub-list
  38. Node groupTail = current;
  39. //reverse
  40. Node prev = null;
  41. Node next = null;
  42. for (int i = 1; i <= k && current != null; i++) {
  43. next = current.next;
  44. current.next = prev;
  45. prev = current;
  46. current = next;
  47. }
  48. // get the new head of the whole list
  49. if (count == 1) {
  50. newHead = prev;
  51. count++;
  52. }
  53. // update the next value of the last node in
  54. // each sub-list
  55. if (previousGroupTail != null) {
  56. previousGroupTail.next = prev;
  57. }
  58. previousGroupTail = groupTail;
  59. }
  60. return newHead;
  61. }
  62. }

链表k个节点反向的更多相关文章

  1. [leetcode]61. Rotate List反转链表k个节点

    类似于找链表的后k个节点 不同的是要把前边的接到后边 public ListNode rotateRight(ListNode head, int k) { //特殊情况 if (head==null ...

  2. 剑指Offer面试题:14.链表的倒数第k个节点

    PS:这是一道出境率极高的题目,记得去年参加校园招聘时我看到了3次,但是每次写的都不完善. 一.题目:链表的倒数第k个节点 题目:输入一个链表,输出该链表中倒数第k个结点.为了符合大多数人的习惯,本题 ...

  3. 链表之求链表倒数第k个节点

    题目描述:输入一个单向链表,输出该链表中倒数第k个节点,链表的倒数第0个节点为链表的尾指针. 最普遍的方法是,先统计单链表中结点的个数,然后再找到第(n-k)个结点.注意链表为空,k为0,k为1,k大 ...

  4. LeetCode Reverse Nodes in k-Group 每k个节点为一组,反置链表

    题意:给一个单链表,每k个节点就将这k个节点反置,若节点数不是k的倍数,则后面不够k个的这一小段链表不必反置. 思路:递归法.每次递归就将k个节点反置,将k个之后的链表头递归下去解决.利用原来的函数接 ...

  5. 剑指offer-第三章高质量的代码(输出该链表中倒数第K个节点)

    题目:输入一个链表,输出这个链表中倒数第K个节点.(代码的鲁棒性) 思路:用两个指针p1和p2,都指向头节点,开始的时候,p2不动,p1移动k-1次,指向第k个节点.此时,如果p1->next! ...

  6. 链表的倒数第K个节点

    题目:输入一个链表,输出该链表中倒数第K个节点.为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个结点. package com.edu; class LinkNode{ //定义一 ...

  7. 删除链表中全部值为k的节点

    1. 问题描写叙述 给定一个单链表,删除当中值为k的全部节点.比如:1→2→6→3→4→5→61 \to 2 \to 6 \to 3 \to 4 \to 5 \to 6,删除当中值为6的节点,返回:1 ...

  8. 13. 求链表倒数第k个节点

    题目:输入1个链表,输出该链表倒数第k个节点,有头指针和尾指针.链表倒数第0个节点是链表的尾指针节点. 代码: /* 尾指针是倒数第0个,则倒数第k个是正数第len-k个,计算len */ #incl ...

  9. 链表中倒数第K个节点

    问题描述: 找出链表中倒数第K个节点 思路分析: 用两个指针,一前一后,保持k个距离,前面的指针移动到末尾,后面的指针就刚好直到第k个节点, 要考虑到k为0,倒数第k个节点不存在的情况. 参考代码: ...

随机推荐

  1. poj 1020 Anniversary Cake(切正方形蛋糕+搜索)

                                                                                                         ...

  2. 【斗地主技巧】斗地主算法逻辑中的天之道<转>

    ******************************************************************** 作者比较喜欢玩斗地主,所以经常搜集一些网友斗地主的心得,下面这 ...

  3. ACM学习-POJ-1143-Number Game

    菜鸟学习ACM,纪录自己成长过程中的点滴. 学习的路上,与君共勉. ACM学习-POJ-1143-Number Game Number Game Time Limit: 1000MS   Memory ...

  4. Linux如何实现开机启动程序详解

    我们假设大家已经熟悉其它操作系统的引导过程,了解硬件的自检引导步骤,就只从Linux操作系统的引导加载程序(对个人电脑而言通常是LILO)开始,介绍Linux开机引导的步骤. 加载内核LILO 启动之 ...

  5. IOS开发之---触摸和手势

    Touch:在与设备的多点触摸屏交互时生成. 响应者对象 响应者对象就是可以响应事件并对事件作出处理.在iOS中,存在UIResponder类,它定义了响应者对象的所有方法.UIApplication ...

  6. 【sql语句】好用的sql语句之项目数据库学习总结

    转载请注明出处:http://blog.csdn.net/pearyangyang/article/details/41115491 这几天学习公司系统的数据流向.主要涉及到几个表的数据. 可是表中的 ...

  7. Bitmap工具类

    一直在使用的一个Bitmap工具类 处理Bitmap和ImageView对象,实现了下面功能: 1.saveBitmap: 把Bitmap对象持久存储到SD卡或手机内存. 2.getViewBitma ...

  8. Sybase配置中文语言支持及字符集

    在windows平台上,Sybase ASE15.0.7安装完成后默认语言是:英语(us_english),默认字符集为:cp850.cp850是一个西欧字符集,虽然也能使用cp850字符集保存汉字, ...

  9. Hortonworks 用于做 Sentimental Analysis的Hiveddl.sql 文件

    The hiveddl.sql script has performed the following steps to refine the data: Converted the raw Twitt ...

  10. django的路由系统

    在django生成的工程项目文件中urls.py文件用于指定路由信息 该文件默认导入以下模块 from confimport from import admin from confimport url ...