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

k个k个的节点反转,即可,用到的记录节点可能比较多,写的比较乱,代码如下所示:

 /**
* 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(k == ) return head;
if(!head || !head->next)
return head;
int len = ;
ListNode * p1 = head;
while(p1){
p1 = p1->next;
len++;
}
//如果k比总长度还要长,那么直接返回即可
if(len < k) return head;
ListNode * prev = NULL;
ListNode * curr = head;
ListNode * tmpNode = NULL;
ListNode * ret = new ListNode(-);
ListNode * helper = curr;
ListNode * helperPre = NULL;
while(len >= k){
for(int i = ; i < k; i++){//区域反转
tmpNode = curr->next;
curr->next = prev;
prev = curr;
curr = tmpNode;
}
if(!ret->next){//第一次手下记下的是首节点的位置
ret->next = prev;
}else{
helperPre->next = prev;//以后将上一部分的尾节点连接到此部分的首节点上面
}
helper->next = curr;
prev = helper;
helper = curr;
helperPre = prev;
len -= k;
}
helperPre->next = curr;//将上次的尾节点域剩下的节点连接起来
return ret->next;
}
};

LeetCode OJ:Reverse Nodes in k-Group(K个K个的分割节点)的更多相关文章

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

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

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

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

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

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

  6. [LeetCode 题解]: Reverse Nodes in K-Groups

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a li ...

  7. leetcode 【 Reverse Nodes in k-Group 】 python 实现

    原题: 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

    1 题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified li ...

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

  10. 【leetcode】Reverse Nodes in k-Group (hard)☆

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

随机推荐

  1. Java线程的几个概念

    线程的生命周期: 新建状态:用new语句创建的线程对象处于新建状态,此时它和其它的java对象一样,仅仅在堆中被分配了内存 就绪状态:当一个线程创建了以后,其他的线程调用了它的start()方法,该线 ...

  2. session与cookie的详解

    在PHP面试中 经常碰到请阐述session与cookie的区别与联系,以及如何修改两者的有效时间. 大家都知道,session是存储在服务器端的,cookie是存储在客户端的,session依赖于c ...

  3. 关于HttpRuntime.Cache的运用

    存Cache方法: HttpRuntime.Cache.Add( KeyName,//缓存名 KeyValue,//要缓存的对象 Dependencies,//依赖项 AbsoluteExpirati ...

  4. 开发人员不可不看的 OBD通讯协议知识

    OBD-II Network Standards» J1850 VPW– Adopted by GM; also known as Class 2.– Adopted by Chrysler (kno ...

  5. SpringBoot 集成Netty实现UDP Server

    注:ApplicationRunner 接口是在容器启动成功后的最后一步回调(类似开机自启动). UDPServer package com.vmware.vCenterEvent.netty; im ...

  6. MySQL——修改数据表

    1.添加单列: ALERT TABLE tbl_name ADD [COLUMN] col_name column_definition [FIRST|AFTER col_name 其中tbl_nam ...

  7. iOS开发进阶 - 富文本正则替换表情

    移动端访问不佳,请访问我的个人博客 最近写项目需要用到富文本解析字符串显示表情,下面是我使用正则替换实现富文本的方式,希望能帮助到大家 先上效果图和demo地址 实现过程中需要用到的知识点 NSReg ...

  8. sbt安装与配置

    下载地址:http://www.scala-sbt.org/download.html 当前版本:sbt-0.13.13.tgz 安装 1.解压并赋予权限 [root@hidden util]# ta ...

  9. HighCharts 在IE8下饼图不显示的问题

    HighCharts饼图用来做数据统计时,在IE8下发现某些图形不能正确显示出来. 在IE8下面会报  'this.renderer.gradients' 为空或不是对象 这样的错误.. 解决方法: ...

  10. 09_MySQL DQL_SQL99标准中的多表查询(外连接)

    # 二.外连接/* 场景:查询值在1个表中出现,在另外1个表中没有出现 特点: 0.也是两张表的字段拼接,分为主表和从表 1.外连接的结果,将显示主表中的所有记录行 如果连接字段在从表中有记录,则显示 ...