Java for LeetCode 025 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
解题思路一:
和k=2情况差不多,考虑到后进先出,可以用Stack实现,JAVA代码如下:
public ListNode reverseKGroup(ListNode head, int k) {
ListNode result = new ListNode(0), index = result,temp=head;
if (head == null)
return null;
Stack<Integer> stack =new Stack<Integer>();
while (temp!=null) {
for (int i = 0; i < k; i++) {
stack.push(temp.val);
temp = temp.next;
if (temp == null&&i<k-1) {
index.next = head;
return result.next;
}
}
while(!stack.isEmpty()){
index.next=new ListNode(stack.pop());
index=index.next;
head=head.next;
}
}
return result.next;
}
解题思路二:
可以利用递归实现,通过指针间相互赋值,实现类似栈的操作,JAVA实现如下:
static public ListNode reverseKGroup(ListNode head, int k) {
ListNode cur = head;
int number = 0;
while (cur != null && number != k) {
cur = cur.next;
number++;
}
if (number == k) {
cur = reverseKGroup(cur, k);
for(int i=0;i<k;i++){
//交换顺序,类似一种栈的操作,可能不太好理解
ListNode temp = head.next;
head.next = cur;
cur = head;
head =temp;
}
head = cur;
}
return head;
}
Java for LeetCode 025 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 ...
- LeetCode 025 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 an ...
- 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 算法思想:基本操作就是链表 ...
- Java for LeetCode 092 Reverse Linked List II
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1-> ...
- Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...
- LeetCode 25 Reverse Nodes in k-Group Add to List (划分list为k组)
题目链接: https://leetcode.com/problems/reverse-nodes-in-k-group/?tab=Description Problem :将一个有序list划分 ...
- 蜗牛慢慢爬 LeetCode 25. Reverse Nodes in k-Group [Difficulty: Hard]
题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
- 【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] 25. 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 ...
随机推荐
- web开发(二十一)之自定义拦截器的使用
转自博客:http://blog.csdn.net/pkgk2013/article/details/51985817 拦截器的作用 拦截器,在AOP(Aspect-Oriented Programm ...
- Oracle导出导入数据库的方式
一.导入导出.dmp文件 利用cmd的操作命令导出,详情如下(备注:方法二是转载网上的教程):1:G:\Oracle\product\10.1.0\Client_1\NETWORK\ADMIN目录下有 ...
- linux中为什么已经是root用户仍不能执行程序
.sh文件 ,获取root权限,提示Permission Denied. 这是因为文件本身没有可执行特性. chmod +x a.sh chmod 755 a.sh
- 【bzoj1046】 HAOI2007—上升序列
http://www.lydsy.com/JudgeOnline/problem.php?id=1046 (题目链接) 题意 给出一个数列,求数列中长度为L的下标字典序最小的上升子序列. Soluti ...
- POJ1947 Rebuilding Roads
Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, n ...
- 洛谷P1156 垃圾陷阱
动规仍然是难关啊 题目描述 卡门――农夫约翰极其珍视的一条Holsteins奶牛――已经落了到“垃圾井”中.“垃圾井”是农夫们扔垃圾的地方,它的深度为D(2<=D<=100)英尺. 卡门想 ...
- 基于redis分布式缓存实现
Redis的复制功能是完全建立在之前我们讨论过的基 于内存快照的持久化策略基础上的,也就是说无论你的持久化策略选择的是什么,只要用到了Redis的复制功能,就一定会有内存快照发生,那么首先要注意你 的 ...
- Matalab IFS分形算法
IFS 算法代码 function IFS_draw(M,p) N=; :length(p); eval(['a',num2str(k),'=reshape(M(',num2str(k),',:),2 ...
- Linux下memcache的安装和启动(转)
memcache是高性能,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度.据说官方所说,其用户包括twitter.digg.flickr等,都是些互联网大腕呀.目前用memca ...
- 快速搭建ssh(最终版)
一个月又忘了.还是做个笔记.自己看下就能想起来... 1.在MyEclipse中新建web Project 2.首先整合struts