题目描述: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 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

代码如下:

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *reverseKGroup(ListNode *head, int k) { if(head == nullptr || head->next == nullptr || k < 2)
return head; ListNode *next_group = head;
for(int i = 0; i < k; i++){
if(next_group)
next_group = next_group->next;
else return head;
} //next_group是未转换的下一组的首地址
//new_next_group是下一组转换之后的首地址
ListNode *new_next_group = reverseKGroup(next_group, k);
ListNode *prev = NULL, *cur = head; while(cur != next_group){
ListNode *next = cur->next;
cur->next = prev ? prev : new_next_group;
prev = cur;
cur = next;
}
return prev;
}
};

LeetCode 025 Reverse Nodes in k-Group的更多相关文章

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

  2. Java for LeetCode 025 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 ...

  3. Reverse Nodes In K Group,将链表每k个元素为一组进行反转---特例Swap Nodes in Pairs,成对儿反转

    问题描述:1->2->3->4,假设k=2进行反转,得到2->1->4->3:k=3进行反转,得到3->2->1->4 算法思想:基本操作就是链表 ...

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

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

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

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

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

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

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

随机推荐

  1. 推荐系统---深度兴趣网络DIN&DIEN

    深度学习在推荐系统.CTR预估领域已经有了广泛应用,如wide&deep.deepFM模型等,今天介绍一下由阿里算法团队提出的深度兴趣网络DIN和DIEN两种模型 paper DIN:http ...

  2. Django实现文件上传功能

    文件上传 关注公众号"轻松学编程"了解更多. 1.创建上传文件夹 在static文件夹下创建uploads用于存储接收上传的文件 在settings中配置,MEDIA_ROOT=o ...

  3. 打印Sql查询语句

    如果在使用了yii的查询语句的话,可以打印本次的Sql,可以用 $model->find()->createCommand()->getRawSql();此语句返回的就是sql查询语 ...

  4. QQ 邮箱日历提醒

    偶然发现 QQ 邮箱有日历的功能,而且可以设置农历并且每年邮件 + 短信 + 微信提醒.这下重要的日子(eg:生日...)就不会忘记啦! 1.找到日历 2.历史提醒 3.新建时间 4.设置时间 5.勾 ...

  5. Spring MVC 9大组件概述

    SpringMVC中的Servlet一共有三个层次,分别是HttpServletBean.FrameworkServlet和 DispatcherServlet.HttpServletBean直接继承 ...

  6. 最长公共子串算法(Longest Common Substring)

    给两个字符串,求两个字符串的最长子串 (例如:"abc""xyz"的最长子串为空字符串,"abcde"和"bcde"的最 ...

  7. c#分割

    Hashtable h = new Hashtable(); ArrayList l = new ArrayList(); string[] lines = System.IO.File.ReadAl ...

  8. tensorflow-gpu2.1缺少libcudnn.so.7

    下载CUDA对应版本的cuDnn. 下载后在cuDnn/cuda/lib64下有libcudnn.so.7这个文件,把它复制到/usr/local/cuda/lib64/下即可

  9. hi-nginx-java并发性能一窥

    欲知hi-nginx-java的并发性能,用jmeter进行测试便知一二. 设定用户数为100000,循环次数为100,ramp-up perio为2: 请求地址为http://localhost/t ...

  10. Go语言内存分配(详述 转)

    一.内存管理简介 1.1 虚拟内存 虚拟内存是当代操作系统必备的一项重要功能,对于进程而言虚拟内存屏蔽了底层了RAM和磁盘,并向进程提供了远超物理内存大小的内存空间.我们看一下虚拟内存的分层设计. 上 ...