reverse-nodes-in-k-group
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相关推荐
- 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 ...
- LeetCode - 25. Reverse Nodes in k-Group
25. Reverse Nodes in k-Group Problem's Link -------------------------------------------------------- ...
- 【重点】LeetCode 25. Reverse Nodes in k-Group
LeetCode 25. Reverse Nodes in k-Group 博客转载自:http://www.cnblogs.com/grandyang/p/4441324.html Solution ...
- 25. Reverse Nodes in k-Group
https://leetcode.com/problems/reverse-nodes-in-k-group/description/ 题意:给一个链表和一个正数k,将链表划分成多个长度为k的链,将这 ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【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 ...
- [LeetCode]k个一组翻转链表(Reverse Nodes in k-Group)
题目描述 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序. 示例 : 给定 ...
- leetcode 25. Reverse Nodes in k-Group | 25. K 个一组翻转链表(Java)
题目 https://leetcode.com/problems/reverse-nodes-in-k-group/ 题解 乍一看以为很容易:每 k 个节点翻转,若剩余不足 k 个,则不变.没有什么技 ...
- [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 ...
- 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 ...
最新文章
- 1501 二叉树最大宽度和高度
- 「学习笔记」多项式相关
- MATLAB使用Python数值和字符变量
- php图片上传报502,PHPStrom上传文件报502错误原因,_PHP教程
- 在女画家杨雁翎的画室派对
- asp连接oracle6,asp下用OracleInProcServer完成对Oracle的连接和操作-ASP教程,数据库相关...
- 全局变量及其含义php,php Server:php超全局变量Server的含义与用法示例
- C++---------【多态性】之【静态多态性】
- TCP 半连接队列和全连接队列满了,怎么破?
- springboot指定属性返回_Spring Boot 最最最常用的注解梳理
- 2021年的理财收益,2022继续加油
- Bypass 360主机卫士SQL注入防御(多姿势)
- 解决IDEA提交代码时提示“Performing Code Analysis...”
- 【S0002】插画大师Laura欧美儿童插画临摹图集363张
- USB总线转串口芯片:沁恒CH340
- 计算思维实践之路(一)
- cas的service管理
- Android修改系统中默认字体大小
- 郁闷的美国百夫长卡中国版
- mysql cbo rbo_oracle的RBO与CBO