Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

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

Note:

  • Only constant extra memory is allowed.
  • You may not alter the values in the list's nodes, only nodes itself may be changed.

题意:

给定一个链表,每k个节点一组,做一次反转。

Solution1:we can still simplify this question into how to reverse a linked list, the only difference is we need to set "dummy" and "null" like the left and right boundary by ourselves.

(1) set a pointer pre as a " dummy " ahead

(2) set a pointer last as a " null " boundary

(3) iteratively move cur into the front(pre.next)

(4) cur = next

(5)iteratively move cur into the front(pre.next) until cur meet the "null" boundary

code:

 /*
Time: O(n)
Space: O(1)
*/
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null) return null;
ListNode dummy = new ListNode(-1);
ListNode pre = dummy;
dummy.next = head;
// to reverse each k-Group, considering pre as a "dummy" ahead
while (pre != null) {
pre = reverse(pre, k);
}
return dummy.next;
} public ListNode reverse(ListNode pre, int k) {
// to reverse each k-Group, considering last as a "null" boundary
ListNode last = pre;
for (int i = 0; i < k + 1; i++) {
last = last.next;
if (i != k && last == null) return null;
} // reverse
ListNode tail = pre.next;
ListNode cur = pre.next.next;
// remove cur to front, then update cur
while (cur != last) {
ListNode next = cur.next;
cur.next = pre.next;
pre.next = cur;
tail.next = next;
cur = next;
}
return tail;
}
}

[leetcode]25. Reverse Nodes in k-Group每k个节点反转一下的更多相关文章

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

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

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

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

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

  5. [leetcode 25]Reverse Nodes in k-Group

    1 题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified li ...

  6. Java [leetcode 25]Reverse Nodes in k-Group

    题目描述: Given a linked list, reverse the nodes of a linked list k at a time and return its modified li ...

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

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

  9. [LeetCode] 25. 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. k  ...

随机推荐

  1. java post json sample

    import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class Te ...

  2. Makefile中的ifeq 多条件使用

    Makefile中的ifeq 多条件使用 网上关于makefile中ifeq的介绍已经很多了,为什么我还要在写这篇文章,因为他们只说了if else两种条件的情况,并没有讲多于两种条件情况的使用. 多 ...

  3. 使用openresty && minio && thumbor 构建稳定高效的图片服务器

    备注: minio 是一个开源的s3 协议兼容的分布式存储,openresty nginx+lua 高性能可扩展的nginx 衍生版,thumbor 基于python 的图片处理服务器,支持图片的裁剪 ...

  4. uniq的坑坑

    很久没有做过文本统计之类的操作了,今天有点任务弄一下,幸亏机智的我列出了全部看了一遍,发现uniq的时候还是有重复的,然后总结了一下 假如我有1.txt这个文本: 10.0.0.1 10.0.0.1 ...

  5. [转]vs2010用 boost.python 编译c++类库 供python调用

    转自:http://blog.csdn.net/wyljz/article/details/6307952 VS2010建立一个空的DLL 项目属性中配置如下 链接器里的附加库目录加入,python/ ...

  6. c 链表和动态内存分配

    兜兜转转又用到了c.c的一些基本却忘记的差不多了(笑哭)!! 动态内存分配 当malloc完将返回的指针类型强制转换成想要的类型后,指针中存有该指针的数据结构,而分配的内存恰好可用于该数据结构. 链表 ...

  7. Excel函数之sumifs应用

    这个函数是用来进行多条件求和的一个函数 示例:根据表格求出“鼎盛书店”2012年度所有图书小计的总和,这里就用sumifs计算 首先定位结果输出单元格,祭出函数. sum_range参数就是筛选后需要 ...

  8. JDK / JRE zip

    Server JRE与JRE的区别: Server JRE一般用于服务器上安装,只有64bit版本,不会安装浏览器插件.自动更新,有监视工具.没有Java Fx和其他开发工具:有安装程序,只是一压缩目 ...

  9. C#编程经验-function advanced params

    function advanced params:outrefparamsnot useful,to use them is a burden,so i dont use themdefects:ou ...

  10. Java中产生随机数的两个方法

    Java中产生随机数的两个方法 一.利用random方法来生成Java随机数. 在Java语言中生成Java随机数相对来说比较简单,因为有一个现成的方法可以使用.在Math类中,Java语言提供了一个 ...