【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 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个节点
思路:其实和旋转整个链表的思路一样,只不过多了一个递归
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverseKGroup(struct ListNode* head, int k) {
if(head==NULL) return NULL;
if(==k) return head;
struct ListNode*ph=head;
struct ListNode*tmp1,*tmp2,*rear,*backup;
tmp1=NULL;
tmp2=NULL;
int i;
for(i=;i<k-&&ph->next!=NULL;i++){
ph=ph->next;
}
rear=ph;
backup=ph->next;
if(i!=k-)
return head;
ph = head;
i=k;
while(i>){
tmp2=ph->next;
ph->next=tmp1;
tmp1=ph;
ph=tmp2;
i--;
}
head->next=reverseKGroup(backup,k);
return rear;
}
【LeetCode】25. Reverse Nodes in k-Group的更多相关文章
- 【LeetCode】25. Reverse Nodes in k-Group (2 solutions)
Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【一天一道LeetCode】#25. Reverse Nodes in k-Group
一天一道LeetCode系列 (一)题目 Given a linked list, reverse the nodes of a linked list k at a time and return ...
- 【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. k ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- [Leetcode][Python]25: Reverse Nodes in k-Group
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Grouphttps://oj. ...
- 【LeetCode】151. Reverse Words in a String
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...
- 【LeetCode】#7 Reverse Integer
[Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...
- 【LeetCode】24. Swap Nodes in Pairs (3 solutions)
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
随机推荐
- 深入浅出SQL注入
原文:深入浅出SQL注入 之前在做学生信息管理系统和机房收费系统的时候,对于SQL注入的问题已经是司空见惯,但是并没有真正的地形象生动的理解SQL注入到底是什么玩意儿.直到这次做牛腩才在牛老师的举例之 ...
- 【转】GitHub 优秀的 Android 开源项目
转自:http://blog.csdn.net/shulianghan/article/details/18046021 主要介绍那些不错个性化的View,包括ListView.ActionBar.M ...
- MVC如何在Pipeline中接管请求的?
MVC如何在Pipeline中接管请求的? 文章内容 上个章节我们讲到了,可以在HttpModules初始化之前动态添加Route的方式来自定义自己的HttpHandler,最终接管请求的,那MVC是 ...
- ftp服务器的搭建
/etc/vsftpd.conf :ftp的配置文件 anonymous_enable=YES:是否支持匿名登录 local_enable=YES:是否支持本地登录 /etc/ftpusers:ftp ...
- ASP.NET Identity登录原理 - Claims-based认证和OWIN
MVC5 - ASP.NET Identity登录原理 - Claims-based认证和OWIN 在Membership系列的最后一篇引入了ASP.NET Identity,看到大家对它还是挺感兴趣 ...
- 使用TotoriseSVN的分支/标签功能
使用TotoriseSVN的分支/标签功能 http://blog.sina.com.cn/s/blog_5383588c0100o1oo.html 原来在新建一个项目文件夹后,再把Trunk.Bra ...
- 没有标准化字符串的后果(IOS)
对于NSString肯定会经常用到,谈谈最近在项目中遇到的一个奇特的现象.如下:我们知道文件系统的命名都是用的字符串,比如你给文件取名“a.pdf”,然后保存文件后,那个文件的名字就真的是 " ...
- GitHub删除文件
删除单个文件 一般情况下,通常可直接在文件管理器中直接把文件删了,或者用rm命令删了 $ rm README.txt 这个时候,工作区是删除了,可版本库还没有.git status命令会立刻告诉你哪些 ...
- 鸟哥的LINUX私房菜基础篇第三版 阅读笔记 一
1. Linux的档案权限与目录配置 一.基础知识: a.分为三类,拥有者(owner).群组(group).其他人(other) b.三个核 ...
- 使用传入的总记录数实现一条sql语句完成分页查询
使用传入的总记录数实现一条sql语句完成分页查询 问题:在传统的分页查询的实现中不可避免的需要两条sql语句,一条用于查询数据一条用于查询总记录数.如下面的实际代码所示: Img1 当然如果使 ...