/**
* 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. elk kibana查询语法

    elk日志系统中kibana查询语法 单项term查询 例: 搜 Dahlen, Malone 字段field查询 field:value 例:city:Keyport, age:26 通配符 ? 匹 ...

  2. maven 监理web war 项目

  3. Eclipse 中打开 python 交互窗口

  4. 虚拟机下Linux操作Ubuntu

    备忘Ubuntu虚拟机环境配置 目录 更新源修改 #支持https的下载 apt 源使用 HTTPS 以确保软件下载过程中不被篡改.因此,我们首先需要添加使用 HTTPS 传输的软件包以及 CA 证书 ...

  5. 为什么需要micro-service构建平台

    最近一直在做micro-service的开发平台建设.由于这是一个实验项目,目前所有工作都得靠自己操刀. 今天在总结用python开发一个web service时,偶有所得,这让我建设micro-se ...

  6. [.net core学习] .net core中的Rijndael取代方法

    The difference (in .NET) between Rijndael and AES is that Rijndael allows the block size to change, ...

  7. 使用MSF打造各种ShellCode

    MSF 生成各种后门 Windows: 生成Windows后门. msfvenom -a x86 --platform Windows -p windows/meterpreter/reverse_t ...

  8. Struts2再爆远程命令执行漏洞![W3bSafe]Struts2-048 Poc Shell及防御修复方案抢先看!

    漏洞概述 Apache Struts是美国阿帕奇(Apache)软件基金会负责维护的一个开源项目,是一套用于创建企业级Java Web应用的开源MVC框架.在Struts 2.3.x 系列的 Show ...

  9. 使用js做LeetCode

    概述 无意中得知了LeetCode这个刷题网站, 深得我意. 其实作为一个程序员, 我是很看重写基础代码的, 因为这个写熟了, 以后学各种语言就不会太困难. 所以我觉得有必要把这件事记下来, 供以后开 ...

  10. Java学习笔记42(序列化流)

    对象中的数据,以流的形式,写入到文件中保存 过程称为写出对象,对象的序列化 ObjectOutputStream将对象写到文件中,实现序列化 在文件中,以流的形式,将对象读取出来, 读取对象,对象的反 ...