【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 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
题解:思路很简单,一步步做就可以了。除了题目要求实现的函数,另外实现了两个函数:
private ListNode[] reverseSub(ListNode head,int k) 该函数将head所指向的长度为k的链表反转,返回一个大小为2的数组,数组中第一个元素是反转后的链表的头节点,第二个元素是反转后的链表的尾节点。
private int getlength(ListNode head) 该函数返回head所指向的链表的长度。
在reverseKGroup函数中,首先计算原链表的长度len,那么需要反转的组的数目就是len/k,接下来调用reverseSub函数len/k次,反转每一段链表,然后根据返回的首尾指针把它们串起来。最后根据len%k是否为0判断链表中是否有不需要反转的元素,如果有,把它们链在链表最后面返回。
代码如下:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
private ListNode[] reverseSub(ListNode head,int k){
ListNode[] answer = new ListNode[2];
answer[1] = head;
ListNode prev = null;
for(int i = 0;i < k;i++){
ListNode temp = head.next;
head.next = prev;
prev = head;
head = temp;
}
answer[0] = prev;
return answer;
}
private int getlength(ListNode head){
int count = 0;
while(head != null){
count ++;
head = head.next;
}
return count;
} public ListNode reverseKGroup(ListNode head, int k) {
int len = getlength(head);
if(len < k)
return head; ListNode answer = null;
ListNode tail = new ListNode(0);
int for_num = len / k;
for(int i = 0;i < for_num;i++){
ListNode h = head; //find next starter
for(int j = 0;j < k;j++)
head = head.next; ListNode[] temp = reverseSub(h, k);
if(answer == null){
answer = temp[0];
tail = temp[1];
}
else {
tail.next = temp[0];
tail = temp[1];
}
} if(len%k != 0)
tail.next = head;
else
tail.next = null; return answer;
}
}
【leetcode刷题笔记】Reverse Nodes in k-Group的更多相关文章
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 【leetcode刷题笔记】Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- [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刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- 【leetcode刷题笔记】Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题:设定一个变量 ...
- 【leetcode刷题笔记】Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【leetcode刷题笔记】Reverse Words in a String
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
随机推荐
- mngoDB 常用语法
http://topmanopensource.iteye.com/blog/1278812### 连接写法:[IP地址:端口号] mongo 192.168.1.161:27017; show db ...
- HTML5中两种方法实现客户端存储数据
HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没有时间限制的数据存储 sessionStorage - 针对一个 session 的数据存储 之前,这些都是由 coo ...
- requests 模块入门玩法和高级玩法
1.安装 pip install requests 2. http://docs.python-requests.org/zh_CN/latest/user/quickstart.html http: ...
- Sql视图创建语句及修改视图
create view [dbo].[AllUsers] as select u.UserId, u.Firstname, u.Lastname, u.ts, am.Email, au.UserNam ...
- java之静态代理
© 版权声明:本文为博主原创文章,转载请注明出处 定义: - 为其他对象提供一种代理以控制对这个对象的访问 组成: 抽象角色:通过接口或抽象类声明真正角色实现的业务方法 真实角色:实现抽象角色,定义真 ...
- C#实战Microsoft Messaging Queue(MSMQ)消息队列(干货)<转>
前言 在使用MSMQ之前,我们需要自行安装消息队列组件!(具体安装方法大家自己搜一下吧) 采用MSMQ带来的好处是:由于是异步通信,无论是发送方还是接收方都不用等待对方返回成功消息,就可以执行余下的代 ...
- BFS 和 DFS
DFS用到递归,BFS要用到一个辅助队列 #include "stdafx.h" #include<iostream> #include<vector> # ...
- 用Jekyll搭建的Github Pages个人博客实践2
依稀记得之前访问喵神的博客很有feel 感谢喵神git上的提供的主题Vno-Jekyll. 创建代码仓库(你的用户名).github.io 将主题Vno-Jekyll下载到本地,解压到刚刚的代码仓库目 ...
- 08 nginx Location总结图解
- 嵌入式开发之qt socket--- qt 封装的socket demo
http://wuyuans.com/2013/03/qt-socket/ http://blog.chinaunix.net/uid-22480862-id-388253.html