【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 ...
 
随机推荐
- 【转】webAPP快速入门
			
WebApp与Native App有何区别呢? Native App: 1.开发成本非常大.一般使用的开发语言为JAVA.C++.Objective-C. 2.更新体验较差.同时也比较麻烦.每一次发布 ...
 - 阿里云1218动态css3代码
			
See the Pen jEWpWm by kujian (@kujian) on CodePen. .room-nav { /* -webkit-animation:roomNavTranslate ...
 - IP地址规划和设计方法
			
IP地址规划和设计方法 无类域间路由技术需要在提高 IP 地址利用率和减少主干路由器负荷两个方面取得平衡 网络地址转换 NAT 最主要的应用是专用网,虚拟专用网,以及 ISP 为拨号用户 提供的服务 ...
 - Varnish 4.0
			
Varnish 4.0 实战 简介 Varnish 是一款高性能且开源的反向代理服务器和 HTTP 加速器,其采用全新的软件体系机构,和现在的硬件体系紧密配合,与传统的 squid 相比,varn ...
 - 一个Shift的后门程序,可以让你可以进入你不知道密码的电脑
			
1.前提 你可以在平时亲身接触状态电脑,哪怕是在电脑主人不在的时候(虽然主人不在,或者关机了,进入电脑是要密码的). 2.原理 利用电脑连续按5次Shift会触发粘滞键,它会运行c:\winows\s ...
 - 2014.3.6-C语言学习小结
			
链表基础: 知识点: 1.链表基础 2.节点的创建和添加 llist_append_node 3.链表的遍历 llist_print_each 4.链表的查找与修改 5.链表的插入与删除 6.链表的销 ...
 - DES加密解密帮助类
			
public class DESCrypto { /// <summary> /// 初始化des实例秘钥及向量 /// </summary> /// <param na ...
 - [转]How WebKit Loads a Web Page
			
ref:https://www.webkit.org/blog/1188/how-webkit-loads-a-web-page/ Before WebKit can render a web pag ...
 - JavaScript之创建对象
			
不定义JQuery插件,不要说会JQuery 一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写$("#"),$("."),写了几年就对 ...
 - c语言:快速排序
			
练手代码(分治实现): input: int input[] = {12,6,3,9,10,6,2}; output: ======================= len = 7 input[0] ...