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. 【8-17】c++学习笔记01

    控制台程序不自动退出方法: system("pause"); getchar() 使用执行 ctrl+F5,开始调试 F5会出现闪退 动态内存分配 //construct c st ...

  2. 使用emmet如何生成lipsum的随机内容

    emmet不会解析(即扩展)大括号中的内容, 它只是把大括号中的内容当成纯粹的字符串, 当成 literal的文本, 不会当成lipsum的缩写, 不会进行扩展. 要扩展, 就必须把lorem, li ...

  3. nginx同一iP多域名配置方法

    文章转自:http://blog.itblood.com/nginx-same-ip-multi-domain-configuration.html 下面的是我本机的配置: server { list ...

  4. 2014 牡丹江区域赛 B D I

    http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=358 The 2014 ACM-ICPC Asia Mudanj ...

  5. [原] Intellij IDEA开发Android,祝还在使用eclipse的早日脱离苦海

    注: 现在推荐使用Android Studio,以后google在Android Studio上个性差异化的东西越来越多, 所以越早使用Android Studio越好,看看更新文档,使我们开发更方便 ...

  6. ASP页面-自动取回数据库中的值生成导航。

    以下为自己总结的一点经验,简单的介绍一下方法,如发现有误,请帮忙指正,谢谢. 一,首先定义调用据库. <% dim objconn,objconnstr set objconn=server.c ...

  7. 利用UIActivityController调用ios系统自带的分享功能,实现微信发布多图的功能

    通过一番查找以后找到一个类UIActivityController,可以调用系统的social.framework中的分享接口.看下面的图就知道了,这个还是挺常见的 微信发布多图 借鉴了CSDN上的一 ...

  8. hdu4751 Divide Groups

    This year is the 60th anniversary of NJUST, and to make the celebration more colorful, Tom200 is goi ...

  9. mysql 删除重复数据,并保存最新一条数据

    删除重复行 DELETE FROM ecm_member_login_session WHERE (number , client_code) IN ( ) AND update_time NOT I ...

  10. javascript高级程序设计---拖拉事件

    拖拉事件 拖拉指的是,用户在某个对象上按下鼠标键不放,拖动它到另一个位置,然后释放鼠标键,将该对象放在那里. 拖拉的对象有好几种,包括Element节点.图片.链接.选中的文字等等.在HTML网页中, ...