【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& ...
 
随机推荐
- 通过rinetd实现端口转发,同时访问阿里云RDS的内外网
			
配置方法如下: 1 wget http://www.boutell.com/rinetd/http/rinetd.tar.gz&&tar -xvf rinetd.tar.gz& ...
 - rplidar 扫描角度设置
			
参考网站:: https://blog.csdn.net/sunyoop/article/details/78302090 https://blog.csdn.net/dzhongjie/arti ...
 - background API
			
语法: background:bg-color bg-image position/bg-size bg-repeat bg-origin bg-clip bg-attachment initial| ...
 - C++中没有定义类的引用。
			
在有时候由于类太大.须要在类在后面定义: 比如: class Y{ void f(X); }; class X{ //一些成员数据和函数 }; //error 由于c++要求不论什么一个变量在引用之前 ...
 - JAVA中的继承特点1
			
*如果子类与父类有相同的字段,则子类中的字段会代替或隐藏父类的字段,子类方法中访问的是子类中的字段(而不是父类中的字段).如果子类方法确实想访问父类中被隐藏的同名字段,可以用super关键字来访问它. ...
 - Android遇到的那些坑
			
1.输出log Log.i("GOOD",v.getId()+"");
 - oracle中can not set解决方法
			
原因:set autotrace on和set trimspool on在pl\sql中使用不了 解决方法:在window环境中,使用cmd命令,sqlplus user_name/password@ ...
 - nginx http proxy 正向代理
			
配置 Nginx Http Proxy 代理服务器,与 [Squid] 功能一样,适用于正向代理 Http 网站. 一,Nginx 正向代理配置文件: server { resolver 8.8.8. ...
 - sql with 递归查询
			
用with实现递归查询 1.数据准备 假定有一个表DiGui,有两个字段Id int ParentId int Id ParentId 4 0 5 0 7 0 2 1 8 5 15 5 9 7 14 ...
 - jq 获取页面中checkbox已经选中的checkbox
			
var array={}; var arrChk=$("input[name='bike']:checked"); if(arrChk.length<=0){ alert(' ...