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. SharpDevelope 在 Windows 7 SP1 with .net framework4.0 下编译时找不到resgen.exe 解决办法

    如果在vs下编译正常,在SharpDevelope下编译报这个错误,可以更改编译时的.netframework版本和C#版本.在 Tool->Project Upgrade 进行项目转换后,一般 ...

  2. PythonStudy——比较运算符 Comparison operator

    1.运算结果为bool类型 print(3 > 5) Output: False 2.可以连比 num = 10 print(1 < num < 20)# 与之上的等价写法是: pr ...

  3. xen虚拟化平台虚拟机在线新加一块磁盘无法识别

    截图 报错:you need to shutdown and then restart the vm before it can access the new disk 添加好磁盘后按照提示重启虚拟机 ...

  4. OpenLDAP主从

    yum -y install compat-openldap必须得安装这个   1:在主上       备份         cp /etc/openldap/slapd.conf /etc/open ...

  5. Maven项目中在properties 中使用 ${} 来引用pom文件中的属性

    比如在pom文件中定义了属性如下: <jdbc.host.global>127.0.0.1</jdbc.host.global> <jdbc.databasename.g ...

  6. 2.2 如何在Visio中写上、下角标

    快捷键:下标[“Ctrl”+ “=”] 上标[“Ctrl”+“shift”+“=”]

  7. PHP批量保存图片到服务器再上传阿里云

    /* * 批量传输产品主图到阿里云 */ public function transferImage(){ $num = 50; $p = isset($this->request->ge ...

  8. sql server紧急状态下登录脚本

    --打开xp_cmdshell功能  EXEC [sys].[sp_configure] @configname = 'xp_cmdshell', -- varchar(35)    @configv ...

  9. 用TreeSet和Comparator给list集合元素去重

    今天在做导入功能时,看到一个感觉很好的去重算法,特分享给大家看看: 其原理利用了以下几点: 1.TreeSet里面不会有重复的元素,所以当把一个List放进TreeSet里面后,会自动去重 2.Tre ...

  10. base64图片内容下载转为图片保存

    网页中的base64图片内容下载后,利用PIL转为图片保存 from skimage.io import imread from PIL import Image from cStringIO imp ...