/**
* Source : https://oj.leetcode.com/problems/reverse-nodes-in-k-group/
*
* Created by lverpeng on 2017/7/12.
*
* 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
*
*/
public class ReverseNodeInKGroup { /***
* 首先得找到翻转的界限,先找到第k个node
*
* 从head开始,依次将下一个node指向上一个node,也就是从前向后改变指向关系
*
* 返回翻转后的最后一个元素,也就是当前元素的上一个节点
*
* @param head
* @param k
*/
public Node reverseKnode (Node head, int k) {
Node end = head;
while (end != null && k > 0) {
end = end.next;
k --;
} // 如果链表总长度小于k
if (k > 0) {
return head;
} Node next = head;
Node last = end;
Node tempNext = null;
while (next != end) {
tempNext = next.next;
next.next = last;
last = next;
next = tempNext;
} return last;
} /**
* 循环翻转,每次翻转k个node
*
* 保存head:第一次翻转后的head就是最终的head
*
* @param head
* @param k
* @return
*/
public Node reverseAll (Node head, int k) {
Node fakeHead = new Node(); // 记录最终的head
fakeHead.next = head;
Node pointer = fakeHead; // 记录当前node
while (pointer != null) {
pointer.next = reverseKnode(pointer.next, k);
for (int i = 0; i < k && pointer != null; i++) {
pointer = pointer.next;
}
} return fakeHead.next;
} private static class Node {
int value;
Node next; @Override
public String toString() {
return "Node{" +
"value=" + value +
", next=" + (next == null ? "" : next.value) +
'}';
}
} private static void print (Node node) {
while (node != null) {
System.out.println(node);
node = node.next;
}
} public static void main(String[] args) {
Node list1 = new Node();
list1.value = 1;
Node pointer = list1;
for (int i = 2; i < 11; i++) {
Node node = new Node();
node.value = i;
pointer.next = node;
pointer = pointer.next;
}
print(list1);
System.out.println();
print(new ReverseNodeInKGroup().reverseAll(list1, 3));
} }

leetcode — reverse-nodes-in-k-group的更多相关文章

  1. [Leetcode] Reverse nodes in k group 每k个一组反转链表

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  2. Reverse Nodes In K Group,将链表每k个元素为一组进行反转---特例Swap Nodes in Pairs,成对儿反转

    问题描述:1->2->3->4,假设k=2进行反转,得到2->1->4->3:k=3进行反转,得到3->2->1->4 算法思想:基本操作就是链表 ...

  3. LeetCode: Reverse Nodes in k-Group 解题报告

    Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...

  4. [LeetCode] Reverse Nodes in k-Group 每k个一组翻转链表

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  5. leetcode Reverse Nodes in k-Group翻转链表K个一组

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

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

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

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

  8. [LeetCode] All Nodes Distance K in Binary Tree 二叉树距离为K的所有结点

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  9. leetcode Reverse Nodes in k-Group python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  10. LeetCode – All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

随机推荐

  1. osg探究补充:DatabasePager类简介

    简介 DatabasePager类,也就是常说的数据库分页技术,简单来说,就是在进行数据库查找时,有可能满足条件的数据很多,为了提高相应速度我们进行数据查找时进行分页查找与显示,当点击下一页时才会进行 ...

  2. iOS 拨打电话三种方式

    ,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示 NSMutableString * str=[[NSMutableString alloc] initWithFo ...

  3. 选择困难症的福音——团队Scrum冲刺阶段-Day 7

    选择困难症的福音--团队Scrum冲刺阶段-Day 7 今日进展 测试代码 将界面设计完后放app使用示意图于此 今日贡献量 严域俊 吴恒佚 曾程 刘辰 邓煜坤 3.5 3.5 3.3 3.6 3 贡 ...

  4. 基于ASP.NET的高校辅导员工作管理系统的设计与实现--论文随笔(四)

    一.基本信息 标题:基于ASP.NET的高校辅导员工作管理系统的设计与实现 时间:2017 出版源:南通理工学院 关键词:ASP.NET; SQL Server; 高校; 管理系统; 辅导员; 二.研 ...

  5. python 实现 Fortran的读取10*0以及换行读问题

    思路,用read来全部读取,然后替换带*的元素来解决.代码如下 import numpy as np import re inf = open('SF.usr') title = inf.readli ...

  6. 游戏脚本编程 文本token解析

    一个数字的组成由以下几个字符 正负号 + -   小数点 .   数字 0-9 比如 3 -3 3.13 -34.2234 但是符号和小数点不会出现多次 那么识别流程用图来表示 则是 整数 浮点数 一 ...

  7. maven 项目中没有src/test/java文件夹

    项目右键->buildPath configure Build Path->点击选项卡Libraries->选中JRE System Library->点击edit->选 ...

  8. PHP字符串函数之 sscanf echo print sprintf vsprintf printf vprintf fprintf vfprintf

    sscanf – 根据指定格式解析输入的字符 echo – 输出一个或多个字符串 print – 输出字符串 sprintf – 返回格式化字符串 vsprintf – 返回格式化字符串 (参数为数组 ...

  9. git代码同步服务器代码需要注意的问题

    魔鬼藏在细节之中,git代码同步服务器代码需要注意的问题 1. 全文件夹覆盖  git pull 当前文件夹 2. 覆盖前做代码备份  mv origin origin_20190404_bak

  10. Springmvc <mvc:cros>和<mvc:intercepters>同时使用时,跨域被拦截了

    问题原因:cros也是使用拦截器实现的,并且拦截器配置最后一个处理,导致在跨域处理之前调用了业务拦截器 解决方案:推荐使用http://software.dzhuvinov.com/cors-filt ...