【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 ...
随机推荐
- 微软Visual Studio "14" CTP 2 发布
微软Visual Studio "14" CTP 2 发布 对于在微软阵营下进行工作的团队来说,拥有最新版本的Visual Studio是提高效率最佳的选择,没有之一. 在本文中, ...
- 传说中的华为Python笔试题——两等长整数序列互换元素,序列和的差值最小(修正)
有两个序列a,b,大小都为n,序列元素的值任意整形数,无序:要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b元素的和]之间的差最小. 1. 将两序列合并为一个序列,并排序,得到source ...
- 对于vijos11.2模拟赛
特意起了个傻逼标题,只是想提醒一下自己以后不要犯逗(所以应该没有什么神犇点进来吧?) T1,T3 当场写的时候就觉得是不可写的,看了题解之后还是觉得不可写,人弱没办法.到了这个时候也懒得管这么难的东西 ...
- Python日期操作
1. 日期输出格式化 所有日期.时间的api都在datetime模块内. 1. datetime => string now = datetime.datetime.now() now.strf ...
- 初试weka数据挖掘
初试weka数据挖掘 Posted on 2013-09-07 13:26 DM张朋飞 阅读(321) 评论(7) 编辑 收藏 偶然间在网上看到了一篇关于weka好的博文,就记录了下来…… weka下 ...
- 线性判别分析算法(LDA)
1. 问题 之前我们讨论的PCA.ICA也好,对样本数据来言,可以是没有类别标签y的.回想我们做回归时,如果特征太多,那么会产生不相关特征引入.过度拟合等问题.我们可以使用PCA来降维,但PCA没有将 ...
- 受众定向-Topic Model
注:这一节我忽略,如果今后有时候,我会整理一份Topic Model的资料来说明,因为原课程中面向的是可能本来就熟悉Topic Model的听众,讲这课只是举个例子,带大家复习一下,所以即使整理出来, ...
- Servlet入门和ServletConfig、ServletContext
Servlet是一门用于开发动态web资源的技术. 若想开发一个动态web资源,需要完成以下2个步骤: 1)编写一个Java类,实现servlet接口: 2)把开发好的Java类部署到web服务器中. ...
- IOS中KVO模式的解析与应用
IOS中KVO模式的解析与应用 最近老翁在项目中多处用到了KVO,深感这种模式的好处.现总结如下: 一.概述 KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修 ...
- TOGAF架构开发方法(ADM)之架构变更管理阶段
TOGAF架构开发方法(ADM)之架构变更管理阶段 1.10 架构变更管理(Architecture Change Management) 企业架构开发方法各阶段——架构变更管理 1.10.1 目标 ...