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. 在windows上使用ssh秘钥连接git服务器

    git部署在centos7上 安装好git后,新建一个用户test(注意要加入git用户组)配置ssh秘钥登录,我的另一篇博客有写配置步骤 重点的地方是在windows系统上使用秘钥登录git服务器 ...

  2. Python Json & Pickle模块

    用于序列化的两个模块 Json,用于字符串 和 python数据类型间进行转换 Pickle,用于python特有的类型 和 python的数据类型间进行转换 Json模块提供了四个功能:dumps. ...

  3. Dockerfile之nginx(六)

    一.Dokcerfile的基本指令   1)From 指定构建镜像的基础镜像 2)MAINTAINER 指定镜像的作者 3)RUN 使用前一条指令创建的镜像生产容器,并在容器中执行命令,执行结束后会自 ...

  4. pac4j探索(一)之buji-pac4j shiro整合Cas实现单点登录

    https://blog.csdn.net/hxm_code/article/details/79181218 https://blog.csdn.net/hxm_code/article/detai ...

  5. 使用 tag 文件定义自定义标签

    ----------------------------------------------------------------------- 在jsp文件中,可以引用tag和tld文件. 1.对于t ...

  6. Spark+Scalar+Mysql

    包:mysql-connector-java-5.1.39-bin.jar 平台:Win8.1 环境:MyEclipse2015 hadoop-2.7.3.tar.gz + winutils.exe ...

  7. 使用css实现时间轴

    本文将使用css来实现一个左右交叉布局的时间轴,效果如下: 使用的都是一些常用的css,代码如下: <!DOCTYPE> <html> <head> <tit ...

  8. Vue 双向数据绑定、事件介绍以及ref获取dom节点

    vue是一个MVVM的框架 M model V view MVVM  model改变会影响视图view,view改变会影响model 双向数据绑定必须在表单里面使用 //我发现在谷歌浏览器翻译后的网页 ...

  9. php多维数组排序

    数组 array(11) { [0] => array(7) { ["food_id"] => string(2) "31" ["food ...

  10. 三台linux集群hadoop,在此上面运行hive

    ---恢复内容开始--- 一,准备 先有三台linux,对hadoop集群的搭建. eddy01:开启一个hdfs的老大namenode,yarn的老大ResourceManager其中进程包括(No ...