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 ...
随机推荐
- Hibernate Generic DAO的介绍安装和使用
java 的包挺多,比c#多 . jar包一个名,解压缩出来又出来又叫另一个名 .搜索起来,内容都分散的很 http://mvnrepository.com maven库搜索 com.googlec ...
- ipcam
ipcam也叫ip network camera,就是基于internet protocol的网络摄像机,同普通摄像头或者网眼的主要区别是ipcam实际上是一台视频服务器和摄像头的集成.ipcam只要 ...
- QT-QWebEngineView-createWindow弹出页面解决
首先要写一个继承QWebEngineView的类 头文件: #ifndef WEBBROWSER_H #define WEBBROWSER_H #include <QWebEngineView& ...
- 第二次spring会议
今天所做之事: 我用C#用DelectText对行数进行了定义,刚开始写代码有点无从下手. 遇到的问题:刚开始用datagridView进行了文本的输入,但是它更适合EXCEL之类的数据计算不符合我们 ...
- 学习Tensorflow的LSTM的RNN例子
学习Tensorflow的LSTM的RNN例子 基于TensorFlow一次简单的RNN实现 极客学院-递归神经网络 如何使用TensorFlow构建.训练和改进循环神经网络
- python class中__init__函数、self
class中包含类内变量以及方法 __init__方法 其前面带有”__“,故此函数被声明为私有方法,不可类外调用. 此方法可以带参数初始化 此方法的首参数必须是”self“(不过”self“也可以换 ...
- 《Linux就该这么学》第七天课程
昨天晚上我找了刘老师决定了报考红帽RHCSA,RHCE认证,我不指望这个认证能给我带来工作上的某些福利,毕竟出去闯靠的是实力外加运气 我只是希望通过这个认证来激励自己! 下面是分享的一些干货! 原创地 ...
- Android开发之Activity
活动(Activity) 活动是最容易吸引用户的地方,它是一种可以包含用户界面的组件,主要用于和用户交互. FirstActivity 手动创建活动 新建一个project,不再选择empty act ...
- 长方体类Java编程题
1. 编程创建一个Box类(长方体),在Box类中定义三个变量,分别表示长方体的长(length).宽(width)和高(heigth),再定义一个方法void setBox(int l, int w ...
- Codeforces gym101612 E.Equal Numbers(贪心)
传送:http://codeforces.com/gym/101612 题意:给出一个大小为n的序列a[i],每次选其中一个数乘以一个正整数,问进行k步操作后最少剩下多少种数字,输出0≤k≤n,所有的 ...