【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& ...
随机推荐
- mongoDB Java SDK CRUD操作例子
Example: package com.sdfwerwer.test; import java.net.UnknownHostException; import com.mongodb.Basi ...
- web-小知识点(随记)
1.position:fixed:窗口定位的时候(在刷新页面时若未出现.得稍等一会的话,就直接把需要定位的内容写在<html>标签的首部.这样页面加载html的时候首先加载fixed定位的 ...
- HTML5 2D平台游戏开发#6地图绘制
此前已经完成了一部分角色的动作,现在还缺少可以交互的地图让游戏看起来能玩.不过在开始之前应当考虑清楚使用什么类型的地图,就2D平台游戏来说,一般有两种类型的地图,Tile-based和Art-base ...
- IIS8应用池重启脚本
重启 IIS8 应用程序池的批处理 批处理很简单:c:\windows\system32\inetsrv\AppCmd.exe stop apppool /apppool.name:"ASP ...
- centos6搭建docker镜像私服
1.创建私服容器 docker run -d -e SETTINGS_FLAVOR=dev -e STORAGE_PATH=/tmp/registry -v /opt/data/registry:/t ...
- shell 获取文件名
1.获取文件名并修改文件名 2.$@ 遍历参数 3.赋值要加"" 4.if 判断注意空格 else后面不能跟then
- KeyboardJS 开发指南 - 与 Three.js 配合使用的捕捉键盘组合键库
KeyboardJS 开发指南 - 与 Three.js 配合使用的捕捉键盘组合键库 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非 ...
- diamond源码阅读-目录监控
PathNode(Path)StandardWatchEventKind(WatchEvent)Watchable(WatchKey WatchService WatchEvent)WatchKey( ...
- TSharding源码阅读-MapperShardingInitializer
/** * 增强Mapper处理总入口:Mapper被mybatis初始化后,在这里做进一步的处理和增强 * * @author qigong on 5/1/15 */ public class Ma ...
- 用象棋的思维趣说IT人的职业发展和钱途
最近我花了不少功夫在学习象棋,也学习了王天一等高手的棋路,感觉IT人的职业和下棋一样,往好了讲,争主动权争实惠只争朝夕,往坏了讲,一步走错得用多步来弥补,如果错误太大未必能弥补回来.在本文里,就用下棋 ...