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 ...
随机推荐
- tensorflow o. 到 tensorflow 1. 部分改变
一下内容为笔者实际应用中遇到的问题,可能(必须)不全,后面会持续更新. (1) tf.nn.run_cell 改为 tf.contrib.rnn (2) tf.reduce_mean 改为 ...
- kbmmw中向服务器端传递对象的一种简单方式
运行环境:delphi 10.2+kbmmw 5.6.20 在kbmmw 的老版本中,要向服务器传送一个本地的对象,一般都需要进行一些转换,例如通过序列化的方式. 在新版的kbmmw中这一切都变的很简 ...
- R语言csv与txt文本读入区分(sep参数)
R语言csv与txt文本读入区分 R语言用来处理数据很方便,而处理数据的第一步是把数据读入内存空间,平时最常用的文本数据储存格式有两种: 一种是CSV(逗号分隔符文本)另一种是TXT(Tab分隔符或空 ...
- 高效率php注意事项
1.尽量静态化: 如果一个方法能被静态,那就声明它为静态的,速度可提高1/4,甚至我测试的时候,这个提高了近三倍. 当然了,这个测试方法需要在十万级以上次执行,效果才明显. 其实静态方法和非静态方法的 ...
- unbuntu14.04下的串口软件monicom的使用
上篇文章写到了将esp-idf中的examples里的helloworld烧写进了esp32的flash里面,本文就讲讲这个例子的测试和一个项目工程的建立. 首先为了得到esp32输出的信息,需要一个 ...
- pwm互补输出 死区设置
void TIM8_PWM_Init(u16 arr,u16 psc){ GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitT ...
- 消息中间件——activeMQ
Activemq使用教程 解压activmq进入bin\win64 启动activemq.bat 启动成功 浏览器访问http://127.0.0.1:8161 创建maven工程 在pom.xml中 ...
- CPU性能分析
CPU性能分析工具 lscpu:查看CPU硬件信息 lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Litt ...
- Internetworking
1 Introduction 所谓的InternetWorking就是将很多网络连接起来,那么在这种连接的网络下我们该如何传送封包呢? 2 IP and Routers 1 IP Datagram H ...
- phpMyAdmin 4.7.x CSRF 漏洞利用
作者:Ambulong phpMyAdmin是个知名MySQL/MariaDB在线管理工具,phpMyAdmin团队在4.7.7版本中修复了一个危害严重的CSRF漏洞(PMASA-2017-9),攻击 ...