题目:

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

题目大意:每当链表中有k个的时候 将其反转

思路:写一个reverse链表的方法,每当符合条件的时候调用方法即可。

代码:(自己第一次写的比较繁琐,下面有精简版)

public class Solution {public ListNode reverseKGroup(ListNode head, int k) {if(head == null)return null;ListNode cur = head;   //记录要反转的头ListNode dep = head;    //保存原始headListNode curPre = new ListNode(0);  // cur的前一个指针 其next为反转后的头ListNode req = null; // 如果有反转的话 记录第一次的头 即最终要返回的int count = k; // 记录next次数 while(head != null){head = head.next;count--;if(count == 0){  //有k个非空 符合要求ListNode temp = reverse(cur, k);if(req == null)  //第一次反转 记录其头req = temp;curPre.next = temp;cur.next = head;count = k;  //计数重置curPre = cur; //保存前一个指针cur = head;continue;}}return req == null ? dep : req;}public ListNode reverse(ListNode head, int k){ListNode dunmy = null;while(k > 0){ListNode temp = head.next;head.next = dunmy;dunmy = head;head = temp;k--;}return dunmy;}
}

精简之后代码(为了简化判断部分,在reverser中 传入了前一个节点)

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;}

参考链接:http://www.cnblogs.com/springfor/p/3864530.html

转载于:https://www.cnblogs.com/TinyBobo/p/4554705.html

[LeetCode-JAVA] Reverse Nodes in k-Group相关推荐

  1. 【重点】LeetCode 25. Reverse Nodes in k-Group

    LeetCode 25. Reverse Nodes in k-Group 博客转载自:http://www.cnblogs.com/grandyang/p/4441324.html Solution ...

  2. 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划分为k ...

  3. LeetCode - 25. Reverse Nodes in k-Group

    25. Reverse Nodes in k-Group Problem's Link -------------------------------------------------------- ...

  4. leetcode 25. Reverse Nodes in k-Group | 25. K 个一组翻转链表(Java)

    题目 https://leetcode.com/problems/reverse-nodes-in-k-group/ 题解 乍一看以为很容易:每 k 个节点翻转,若剩余不足 k 个,则不变.没有什么技 ...

  5. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  6. 25. Reverse Nodes in k-Group

    https://leetcode.com/problems/reverse-nodes-in-k-group/description/ 题意:给一个链表和一个正数k,将链表划分成多个长度为k的链,将这 ...

  7. LeetCode之Reverse String II

    1.题目 Given a string and an integer k, you need to reverse the first k characters for every 2k charac ...

  8. LeetCode 974. 和可被 K 整除的子数组(哈希map)

    1. 题目 给定一个整数数组 A,返回其中元素之和可被 K 整除的(连续.非空)子数组的数目. 示例: 输入:A = [4,5,0,-2,-3,1], K = 5 输出:7 解释: 有 7 个子数组满 ...

  9. LeetCode 862. 和至少为 K 的最短子数组(前缀和+deque单调栈)

    1. 题目 返回 A 的最短的非空连续子数组的长度,该子数组的和至少为 K . 如果没有和至少为 K 的非空子数组,返回 -1 . 示例 1: 输入:A = [1], K = 1 输出:1示例 2: ...

  10. LeetCode 541. Reverse String II

    题目: Given a string and an integer k, you need to reverse the first k characters for every 2k charact ...

最新文章

  1. Inside C++ object Model--构造函数
  2. python用编程软件_Python编程工具pycharm的使用
  3. 舞力全开加速_国行舞力全开评测:丝滑得不像是育碧服务器!
  4. CryptoAPI 学习
  5. 读书笔记1 : program paradigm
  6. sqoop2 java api实现_Sqoop2 Java客户端API指南
  7. Java提高篇 —— String缓冲池
  8. 贴花纸怎么贴_陶瓷贴花纸DIY怎么做?
  9. 通知 notification
  10. 统计xml文件中的标签出现框数及出现过的图片数
  11. MFC中的CFileFind类
  12. python画二维温度云图_怎么用Python画出好看的词云图?
  13. 368.最大整数子集
  14. 味美多网址导航php,味多美网址导航源码程序按来路自动显示 2010.0329
  15. Android 录屏(录像)录制视频自定义输出视频分辨率,设置最合适尺寸;Android Mediacodec 录屏输出视频被缩小,Android 录屏全屏,录屏自定义尺寸,录屏录像黑边
  16. 用分数表示循环小数(C#版)
  17. python中compile的作用_Python compile函数有什么用?
  18. ArcGIS问题:dbf shp shx sbn sbx mdb adf等类型的文件的解释
  19. 电子书沦为“压泡面”神器,其实高端电子书就该从这两个里边选
  20. 社交网络分析方法以及基本图例

热门文章

  1. 转:PHP Liunx 服务安全防范方案
  2. Suse es 11sp2 双网卡冗余
  3. .net c# 正则表达式 平衡组/递归匹配
  4. JavaScript入门(一)
  5. Python的if __name__ == ‘__main__‘:的作用
  6. DSP48E Slice
  7. Xilinx FPGA中SRL(移位寄存器)资源
  8. 场效应晶体管的几点使用知识!
  9. 从名称认识电容在电路中的作用
  10. 代码荣辱观-以运用风格为荣,以随意编码为耻