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.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode* node = head;
//第一个k个
for(int i=0;i<k;i++){
if(!node)
return head;//不到k个长
node = node->next;
}
//node是last的后一个
ListNode* new_head = reverse(head,node);
//关键递归
head->next = reverseKGroup(node,k);
return new_head;
} //翻转链表 1 2 3
ListNode* reverse(ListNode* head,ListNode* last){
//last是翻转链表的后一个节点
ListNode *pre=last, *cur=head;
while(cur != last){
ListNode* Next = cur->next;
cur->next = pre;
pre = cur;
cur = Next;
}
return pre;
}
};

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

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

  5. K组翻转链表 · Reverse Nodes in k-Group

    [抄题]: 给你一个链表以及一个k,将这个链表从头指针开始每k个翻转一下.链表元素个数不是k的倍数,最后剩余的不用翻转. [思维问题]: [一句话思路]: // reverse head->n1 ...

  6. Leetcode 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. If ...

  7. LeetCode Reverse Nodes in k-Group 每k个节点为一组,反置链表

    题意:给一个单链表,每k个节点就将这k个节点反置,若节点数不是k的倍数,则后面不够k个的这一小段链表不必反置. 思路:递归法.每次递归就将k个节点反置,将k个之后的链表头递归下去解决.利用原来的函数接 ...

  8. [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

  9. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

随机推荐

  1. 接入WxPusher微信推送服务出现错误:Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

    背景 使用WxPusher微信推送服务 ,可以及时的将服务的一些运行异常信息,发送到自己的微信上,方便了解服务的运行状态(PS:这个服务是免费的). 你可以在这里看到WxPusher微信推送服务的接入 ...

  2. [Leetcode题解]2. 两数相加-链表遍历和重构

    1. 审题leetcode 02 add-two-numbers​ 我们先看一下题目,如下  : 链表的从前往后为数字的低位到高位,模拟加法手算过程,从前往后遍历即可, 注意每个数字0-9,进位要处理 ...

  3. go 加锁

    package mainimport ( "fmt" "math/rand" "sync" "time")var tic ...

  4. nfs4使用中的防火墙配置

    一,查看本地centos的版本: [root@localhost lib]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core) ...

  5. git tag的应用

    一,git的tag是什么? tag就是给commit起一个容易记忆容易理解的名字 说明:架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectfores ...

  6. virtualbox 网络地址转换(NAT)

    网络地址转换 虚拟机可以访问主机 通过主机请求外网 但是主机不能请求虚拟机 所以要配置端口转发才行 host-only模式下 不同网段的不同虚拟机也可以互相ping通  比如 192.168.33.1 ...

  7. java安全编码指南之:Thread API调用规则

    目录 简介 start一个Thread 不要使用ThreadGroup 不要使用stop()方法 wait 和 await 需要放在循环中调用 简介 java中多线程的开发中少不了使用Thread,我 ...

  8. LeakCanary检测内存泄漏

    内存泄漏原因: 线程造成的内存泄漏 Handler造成的内存泄漏 单例导致内存泄露 静态变量导致内存泄露 非静态内部类导致内存泄露 未取消注册(BroadcastReceiver )或回调导致内存泄露 ...

  9. H5头部内容

    <head> <meta charset="utf-8"> <title>口袋小鼠</title> <meta name=&q ...

  10. windows下安装RabbitMq和常用命令

    ----RabbitMq安装-----windows下安装:(1)首先windows下安装好了erlang和rabbitmq.如下地址同时下载和安装:Erlang:http://www.erlang. ...