leetcode — reverse-nodes-in-k-group
/**
* 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的更多相关文章
- [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 ...
- 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 算法思想:基本操作就是链表 ...
- 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 ...
- [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 ...
- 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 ...
- LeetCode Reverse Nodes in k-Group 每k个节点为一组,反置链表
题意:给一个单链表,每k个节点就将这k个节点反置,若节点数不是k的倍数,则后面不够k个的这一小段链表不必反置. 思路:递归法.每次递归就将k个节点反置,将k个之后的链表头递归下去解决.利用原来的函数接 ...
- 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 ...
- [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 ...
- leetcode Reverse Nodes in k-Group python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- 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 ...
随机推荐
- Luogu3119 草鉴定-Tarjan+Topsort
Solution 简单的$Tarjan$题. 有大佬现成博客 就不写了 → 传送门 Code #include<cstdio> #include<cstring> #inclu ...
- Netsharp总体设计
阅读本文请先阅读如下两篇文章 什么是企业软件 Netsharp什么 Netsharp总体设计 1.1 Netsharp终端形式 Netsharp基于.NET平台,支持的产品形态有三种: 北极熊 ...
- AX_NumberSeq
NumberSeq::newGetNum(NumberSeqReference::findReference(typeId2ExtendedTypeId(typeid(InventTransId))) ...
- springMVC学习 十二 拦截器
一 拦截器概述 拦截器技术比较像java web技术中的过滤器技术,都是发送 请求时被拦截器拦截,在控制器的前后添加额外功能.但是和Spring中的Aop技术是由区别的.AOP 在特定方法前后扩充(一 ...
- 学习:java集合
java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE, JavaME, Jav ...
- python中 os._exit() 和 sys.exit(), exit(0)和exit(1) 的用法和区别
os._exit() 和 sys.exit() os._exit() vs sys.exit() 概述 Python的程序有两中退出方式:os._exit(), sys.exit().本文介绍这两种方 ...
- Solidity合约记录——(一)如何寻找以太坊真实Solidity源码
在自主学习Solidity智能合约的过程中,第一份入手资料无疑是官方文档.感谢前辈们还能提供出文档的中文翻译,作为我入门的第一手资料:文末附上有用的学习链接{持续更新中} 阅读完基础文档同时上手合约后 ...
- UniGUI的布局使用说明
(unigui的页面布局还是很强大的,基本什么的排版都能搞好.前面部分为原文章翻译,翻译不一定很准确,就能看吧,后面有使用说明,有什么不明白的欢迎加我QQ(910300653)一起交流学习) 一.布局 ...
- wampserver 的默认首页设置
# wampserver 首页顺序设置 <IfModule dir_module> DirectoryIndex index.php default.php index.html inde ...
- tomcat虚拟目录配置
Tomcat6.0虚拟目录配置[转] 设虚拟目录 "site",通过 http://localhost:8080/site 访问物理路径 D:"site 文件夹里面的内容 ...