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

将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; ListNode *front=new ListNode();
front->next=head; ListNode *cur,*next,*result;
cur=head;
result=head; while(cur!=NULL)
{
int count=;
ListNode *tmp=cur; //当前元素以及后续元素之和是否够k个
while(cur!=NULL)
{
cur=cur->next;
if(cur==NULL) break; count++;
if(count==k)
{
break;
}
} if(count==k)
{
//此时cur指向需要翻转的最后一个元素,last表示下次开始时的元素
ListNode *last=cur->next;
cur=tmp; //表示是否需要更新头结点,作为返回值
bool isfirst=front->next==head; //每次把当前的元素移动到开头
while(cur->next!=last)
{
//获得下一个元素
next=cur->next;
//当前元素指向下下个元素
cur->next=next->next;
//下一个元素移动到开头
next->next=front->next;
//指向开头元素
front->next=next; if(isfirst) result=front->next;
}
}
else
{
break;
} front=cur;
cur=last;
} return result;
}
};

【leetcode】Reverse Nodes in k-Group的更多相关文章

  1. 【LeetCode】Reverse Nodes in k-Group(k个一组翻转链表)

    这是LeetCode里的第25道题. 题目要求: 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最 ...

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

  3. 【leetcode】698. Partition to K Equal Sum Subsets

    题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...

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

  5. 【Leetcode】【Hard】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 ...

  6. 【LeetCode】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  7. 【leetcode】Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

  8. 【leetcode】Reverse Words in a String

    今天第一次在leetcode上提交了一个题目,据说这个网站基本上都是名企面试笔试题,今天无意一进去就看到第一题居然就是昨天的腾讯实习生笔试题,赶紧注册了个账号做题. 题目描述: Given an in ...

  9. 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 算法思想:基本操作就是链表 ...

随机推荐

  1. HashSet与HashMap的区别

    本文由 ImportNew - 唐小娟 翻译自 Javarevisited.欢迎加入翻译小组.转载请见文末要求. HashMap和HashSet的区别是Java面试中最常被问到的问题.如果没有涉及到C ...

  2. MFS文件系统

    一.MFS文件系统概论 MFS是linux下的开源存储系统,是由波兰人开发的.MFS文件系统能够实现RAID的功能,不但能够节约存储成本,而且不逊于专业的存储系统,能够实现在线扩展.MFS是一种半分布 ...

  3. [Asp.net mvc]jquery.form.js无刷新上传

    写在前面 最近在自己的网盘项目中想用ajax.beginform的方式做无刷新的操作,提交表单什么的都可以,但针对文件上传,就是个鸡肋.在网上查找了发现很多人都遇到了这个问题,大部分都推荐使用jque ...

  4. oracle中一些用法总结

    1. case用法: --简单case函数 case sex when '1' then 'boy' when '2' then 'girl' else '其他' end; --case搜索函数 ca ...

  5. PostgreSQL表空间、数据库、模式、表、用户/角色之间的关系

    看PostgreSQL9的官方文档,我越看越迷糊,这表空间,数据库,模式,表,用户,角色之间的关系怎么在PostgreSQL里这么混乱呢?经过中午的一个小实验,我逐渐理清了个中来龙去脉.下面我来还原我 ...

  6. 响应性web设计实战总结(二)

    响应性web设计实战总结(二) 阅读目录 背景知识: Gulp-less安装及配置如下 对响应性web总结,之前总结过2篇文章:可以看如下: http://www.cnblogs.com/tugenh ...

  7. 安装SQL Server 2012 『企业中文版』

    安装 SQL Server 前,请详细参阅:计划安装SQL Server2012需求详细http://www.cnblogs.com/chhuang/p/3623198.html 安装 SQL Ser ...

  8. Ali相关面试题

    接到的电话面试,人比较随和,当时IOS有一段时间没怎么碰了,因为近期一直在用C++,QT做IM.很多回答我都扯到了C++上,所以可能没戏- -! 回想一下,大概有如下几个问题:(都是很常见的问题) 1 ...

  9. 开源一个动态解析protobuf的工具

    好久没写博客了,主要是这一年技术没啥长进都打杂了,还有就是生活琐事越来越多,人也越来越懒了…… 之前项目中用到了Protobuf,然后测试发现这玩意不好测,总不能每次定个协议或者改下都要编译Java代 ...

  10. Codeforces Round #270 1002

    Codeforces Round #270 1002 B. Design Tutorial: Learn from Life time limit per test 1 second memory l ...