https://leetcode.com/problems/reverse-nodes-in-k-group/

https://leetcode.com/mockinterview/session/result/xjlzcwc/

链表分批倒置,还是有点绕的。用了三个临时变量来处理。需要细心,也需要掌握类似经验。

package com.company;import java.util.*;class ListNode {int val;ListNode next;ListNode(int x) { val = x; }
}class Solution {public ListNode reverseKGroup(ListNode head, int k) {if (k <= 1) {return head;}ListNode ret = new ListNode(0);ret.next = head;ListNode cur = ret;ListNode next = head;ListNode first = null;ListNode second = null;ListNode third = null;while (true) {int i=0;for (; i<k; i++) {if (next == null) {break;}next = next.next;}if (i < k) {break;}first = cur.next;second = first.next;for (i=0; i<k-1; i++) {third = second.next;second.next = first;first = second;second = third;}cur.next.next = next;third = cur.next;cur.next = first;cur = third;}return ret.next;}
}public class Main {public static void main(String[] args) throws InterruptedException {System.out.println("Hello!");Solution solution = new Solution();// Your Codec object will be instantiated and called as such:ListNode ln1 = new ListNode(1);ListNode ln2 = new ListNode(2);ListNode ln3 = new ListNode(3);ListNode ln4 = new ListNode(4);ListNode ln5 = new ListNode(5);ln1.next = ln2;ln2.next = ln3;ln3.next = ln4;ln4.next = ln5;ListNode ret = solution.reverseKGroup(ln1, 3);for (int i=0; i<5; i++) {System.out.printf("ret:%d\n", ret.val);ret = ret.next;}System.out.println();}}

reverse-nodes-in-k-group相关推荐

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

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

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

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

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

  4. 25. Reverse Nodes in k-Group

    https://leetcode.com/problems/reverse-nodes-in-k-group/description/ 题意:给一个链表和一个正数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. 【leetcode】25. Reverse Nodes in k-Group 链表按K分段逆序

    1. 题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified li ...

  7. [LeetCode]k个一组翻转链表(Reverse Nodes in k-Group)

    题目描述 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序. 示例 : 给定 ...

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

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

  9. [LeetCode-JAVA] 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 ...

  10. 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 ...

最新文章

  1. 1501 二叉树最大宽度和高度
  2. 「学习笔记」多项式相关
  3. MATLAB使用Python数值和字符变量
  4. php图片上传报502,PHPStrom上传文件报502错误原因,_PHP教程
  5. 在女画家杨雁翎的画室派对
  6. asp连接oracle6,asp下用OracleInProcServer完成对Oracle的连接和操作-ASP教程,数据库相关...
  7. 全局变量及其含义php,php Server:php超全局变量Server的含义与用法示例
  8. C++---------【多态性】之【静态多态性】
  9. TCP 半连接队列和全连接队列满了,怎么破?
  10. springboot指定属性返回_Spring Boot 最最最常用的注解梳理
  11. 2021年的理财收益,2022继续加油
  12. Bypass 360主机卫士SQL注入防御(多姿势)
  13. 解决IDEA提交代码时提示“Performing Code Analysis...”
  14. 【S0002】插画大师Laura欧美儿童插画临摹图集363张
  15. USB总线转串口芯片:沁恒CH340
  16. 计算思维实践之路(一)
  17. cas的service管理
  18. Android修改系统中默认字体大小
  19. 郁闷的美国百夫长卡中国版
  20. mysql cbo rbo_oracle的RBO与CBO

热门文章

  1. Maven MyEclipse创建web项目没有src/maim/java
  2. ibatis常用16条SQL语句
  3. 多个Wyze 摄像头漏洞可导致攻击者接管设备并访问视频
  4. 微软 Edge bug 导致黑客窃取用户在任意站点的机密信息,颁发2万美元奖金
  5. 深入理解SpringBoot的过滤条件--AutoConfigure
  6. SteveY对Amazon和Google平台的吐槽
  7. 万亿安防市场前景可期 未来四大发展趋势分析
  8. 1038 一元三次方程求解
  9. 关于超过255台电脑的内网IP规划问题
  10. 新站如何迅速提高流量和被百度收录的技巧绝版