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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 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 算法思想:基本操作就是链表 ...

  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-> ...

  5. Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)

    Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...

  6. 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划分 ...

  7. 蜗牛慢慢爬 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. ...

  8. 【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 ...

  9. [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  ...

随机推荐

  1. yield实例

    如下 # __author__ = liukun # coding:utf-8 def it(): print ('hello') yield 1 yield 1 a= it() print(&quo ...

  2. 使用属性表:VS2013上配置OpenCV

    以前,windows下配置OpenCV一直不太方便:总是要手动添加lib,添加include,还要配置PATH使得程序运行时候能找到dll文件. 每次新建一个使用OpenCV的工程都要手动添加,很麻烦 ...

  3. BZOJ-2038 小Z的袜子(hose) 莫队算法

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MB Submit: 5573 Solved: 2568 [Subm ...

  4. schemaLocation value = 'xxxxxxxxxxxx' must have even number of URI's

    这是因为没有加上Spring的版本号,加上就行了,如: http://www.springframework.org/schema/beans/spring-beans.xsd -3.2.2 http ...

  5. 如何在 Ubuntu 14.04 里面配置 chroot 环境

    你可能会有很多理由想要把一个应用.一个用户或者一个环境与你的 Linux 系统隔离开来.不同的操作系统有不同的实现方式,而在 Linux 中,一个典型的方式就是 chroot 环境. 在这份教程中,我 ...

  6. 从js的repeat方法谈js字符串与数组的扩展方法

    js将字符串重复N次的repeat方法的8个版本 /* *@desc: 将一个字符串重复自身N次 */ //版本1:利用空数组的join方法 function repeat(target, n) { ...

  7. HD 1533 Going Home(最小费用最大流模板)

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  8. auto,register,static实例

    #include <stdio.h>int main() {    auto int i = 0;    register int j = 0;    static int k = 0;  ...

  9. Spring学习7-Spring整合Hibernate

    一.Springl为什么要整合Hibernate   二者的整合主要是把hibernate中核心的一些类型交给spring管理,这些类型主要包括sessionFactory. transactionM ...

  10. Boost的状态机库教程(1)

    介绍 Boost状态机库一个应用程序框架,你可以用它将UML状态图快速的转换为可执行的c++代码,而不需要任何的代码生成器.它支持几乎所有的UML特征,可以直接了当的转换,并且转换后的c++代码就像对 ...