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.

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个一组,按顺序每次翻转一组。

  对于每一组,先找到组内的第k个节点,如果找不到则说明长度小于k,无须翻转,返回null。

  翻转时,先定位组内的头尾节点n1和nk。定义两个指针prev和curr,每次将curr.next变成prev后,两个指针分别后移一位,直到prev为nk,此时组内节点已全部翻转,只需再处理头尾节点即可。处理头尾节点,需要将n1.next指向curr(即nk+1),再将head.next指向nk,最后返回n1。

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null || k < 2) {
return head;
} ListNode dummy = new ListNode(0);
dummy.next = head;
head = dummy; while (head != null) {
head = reverseK(head, k);
} return dummy.next;
} public ListNode reverseK(ListNode head, int k) {
ListNode nk = head;
for (int i = 0; i < k; i++) {
nk = nk.next;
if (nk == null) {
return null;
}
} ListNode n1 = head.next; ListNode prev = null;
ListNode curr = n1;
while (prev != nk) {
ListNode temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
n1.next = curr;
head.next = nk;
return n1;
}
}

[LeetCode] 25. Reverse Nodes in k-Group ☆☆☆的更多相关文章

  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 Add to List (划分list为k组)

    题目链接: https://leetcode.com/problems/reverse-nodes-in-k-group/?tab=Description   Problem :将一个有序list划分 ...

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

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

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

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

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

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

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

随机推荐

  1. centos上搭建git服务--4

    Git是目前世界上最先进的分布式版本控制系统(没有之一).使用Svn的请参考<版本控制-svn服务器搭建和常用命令(centos 6.3)>,下面介绍Git的常用命令 常用命令 简单版 升 ...

  2. php命名空间学习笔记。

    为什么要用命名空间? 在PHP中,命名空间用来解决在编写类库或应用程序时创建可重用的代码如类或函数时碰到的两类问题: 用户编写的代码 与  PHP内部的类/函数/常量或第三方类/函数/常量之间的名字冲 ...

  3. sprint2 团队贡献分

    团队名:在考虑 团队贡献分: 102 杨晶晶:17 106 邹育萍:18 114 纪焓:16 116 黄敏鹏:28 117 郑培轩:26 138 曾昱霖:15 最新项目的github地址: https ...

  4. HDU 5265 pog loves szh II 二分

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5265 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  5. lintcode-221-链表求和 II

    221-链表求和 II 假定用一个链表表示两个数,其中每个节点仅包含一个数字.假设这两个数的数字顺序排列,请设计一种方法将两个数相加,并将其结果表现为链表的形式. 样例 给出 6->1-> ...

  6. JavaScript与OC的交互-WebViewJavascriptBridge

    WebViewJavascriptBridge实现了在使用UIWebView时JS与ios 的Objective-C nativecode之间的互相调用, 支持的功能有消息发送.接收.消息处理器的注册 ...

  7. 对scrum站立会议的理解

    个人理解:首先我不明白scrum的含义,查了一下,scrum是迭代式增量软件开发过程,通常用于敏捷开发.scrum包括了一系列实践和预定义角色的过程骨架.scrum中的主要角色包括同项目经理类似的sc ...

  8. 【BioCode】将多个蛋白质序列分成单个的txt文档

    代码说明: fasta格式的蛋白质序列,一个txt里面有很多蛋白质序列,计算ss.pssm或disorder score时候都需要单条计算,需要分开. 分割前: 分割后: show you the c ...

  9. Java实现的词频统计

    要求: 1.读取文件: 2.记录出现的词汇及出现频率: 3.按照频率降序排列: 4.输出结果. 概要: 1.读取的文件路径是默认的,为了方便调试,将要统计的文章.段落复制到文本中即可:2.只支持英文: ...

  10. [转帖]IBM收购Red Hat

    来源cnbeta:https://www.cnbeta.com/articles/tech/782009.htm 2018年10月28 日,IBM 宣布收购 Linux 巨头 Red Hat.公告中称 ...