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. Oracle写函数读写日志实例

    1.用DBA登录赋权限create or replace directory D_OUTPUT as 'D:\TEMP'; grant read,write on directory D_OUTPUT ...

  2. BZOJ-1066 蜥蜴 最大流+拆点+超级源超级汇

    1066: [SCOI2007]蜥蜴 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 2582 Solved: 1272 [Submit][Status] ...

  3. 点击div区域以外部分,div区域隐藏

    核心思想: 监听body的click事件,事件触发的时候判断是否发生在弹出的div上,如果不在,关闭弹层 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTM ...

  4. poj 3070 矩阵快速幂模板

    题意:求fibonacci数列第n项 #include "iostream" #include "vector" #include "cstring& ...

  5. jquery------.cycle的使用

    代码下载地址:http://malsup.github.io/jquery.cycle.all.js 把里面的代码复制到jquery.cycle.all.js里面 index.jsp <scri ...

  6. Fedora 20下配置samba服务器

    1 安装samba [root@localhost ~]# yum –y install samba   ← 通过网络安装samba yum -y install samba-client    // ...

  7. Java,PostgreSQL时间范围查询

    遇到一坑:对于如下代码 select * from order_mileagefuel where date > '2015-11-1' and date< '2015-11-5' 在Po ...

  8. logback logback.xml常用配置详解(三)

    logback logback.xml常用配置详解 <filter> <filter>: 过滤器,执行一个过滤器会有返回个枚举值,即DENY,NEUTRAL,ACCEPT其中之 ...

  9. 一个简单的javascript深拷贝

    var extendDeep = function(parent,child){ var i, toStr = Object.prototype.toString, astr = '[object A ...

  10. 关于js函数中的异步编程

    大家都说js 是单线程的应用,但是随着技术的发展,js的发展已经不仅仅局限于单线程了.因为现在很多都是异步了,所谓的异步,就是类似于ajax,写了一个回调函数,当我的服务还在这个地方的时候,等着他去排 ...