问题:

以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. FFmpeg深入分析之零-基础 <第一篇>

    FFmpeg是相当强大的多媒体编解码框架,在深入分析其源代码之前必须要有基本的多媒体基础知识,否则其源代码会非常晦涩难懂.本文将从介绍一些基本的多媒体只是,主要是为研读ffmpeg源代码做准备,比如一 ...

  2. 【Xamarin挖墙脚系列:关闭 OS X El Capitan 中 SIP 安全设置功能】

    比如需要修改内核配置文件: com.apple.Boot.plist 那么我们需要解锁权限. 禁止SIP模式,那么就可以修改此文件了. 在 OS X El Capitan 中有一个跟安全相关的模式叫 ...

  3. 蓝桥杯 六角形中填置1~12个数字 dfs

    如图[1.png]所示六角形中,填入1~12的数字. 使得每条直线上的数字之和都相同. 图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少? 请通过浏览器提交答案,不要填写多余的内容. ...

  4. Median of Two Sorted Arrays (找两个序列的中位数,O(log (m+n))限制) 【面试算法leetcode】

    题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sor ...

  5. 对中级Linux 用户非常有用的20 个命令

    也许你已经发现第一篇文章非常的有用,这篇文章是继对初级Linux用户非常有用的20个命令的一个延伸. 第一篇文章的目的是为新手准备的而这篇文章则是为了Linux的中高级用户.在这里你将学会如何进行自定 ...

  6. Objective C - UIColor

    UIColor+Hex.h #import <UIKit/UIKit.h> @interface UIColor (Hex) + (UIColor *) colorWithHexStrin ...

  7. 第五章 Logistic回归

    第五章 Logistic回归 假设现在有一些数据点,我们利用一条直线对这些点进行拟合(该线称为最佳拟合直线),这个拟合过程就称作回归. 为了实现Logistic回归分类器,我们可以在每个特征上都乘以一 ...

  8. XML.ObjTree -- XML source code from/to JavaScript object like E4X

    转载于:http://www.kawa.net/works/js/xml/objtree-try-e.html // ========================================= ...

  9. 在IIS上Office Word下载失败,检索 COM 类工厂中 CLSID 为000209FF的组件失败,80070005 拒绝访问。

    最近在做一个网站时,有一个下载word文档功能,在本地直接调试是可以下载的,但部署到IIS上就出现问题了. 出现问题如下:Error:下载简历方法出错:检索 COM 类工厂中 CLSID 为 {000 ...

  10. json 去空值与缩进

    var jSetting = new Newtonsoft.Json.JsonSerializerSettings(); //忽略值为null的 jSetting.NullValueHandling ...