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. WEBSTORM 打开多个项目的方法

    WebStorm默认情况下一次只能打开一个项目,这点很不爽,其实是可以设置的. 方法: File -> settings -> Directories -> Add Content ...

  2. js实现点击增加文本输入框

    html代码: <ul id="ulid21" > <li id="li11" >问卷选项设置:</li> </ul& ...

  3. Struts2中ModelDriven的陷阱及其预防

    页面表单 <form action="updateInput.action" method="post"> <input type=" ...

  4. jsp系统时间和时间对比(活动结束不结束)

    jsp页面拿到系统时间 <jsp:useBean id="now" class="java.util.Date" /> <fmt:format ...

  5. 新浪微博客户端(63)-使用block进行链式编程

    Person.h #import <Foundation/Foundation.h> @interface Person : NSObject - (Person *(^)())study ...

  6. 使用code标签获得类似代码段的效果

    几乎所有的浏览器都支持 code标签 code标签, 顾名思义,就是代码标签, imply tell browser, that 后面的部分是表示计算机代码. 因此, 浏览器可以根据自己的特点来显示这 ...

  7. ios如何普安短图片类型

    很多时候需要知道服务器返回的图片是.png还是.jpg或者是.git, 两种方式 1,获取扩展名 //图片    NSString *image = @"4351141241.GIT&quo ...

  8. 东京区域2012-2014主要消费产品价格参考表——Excel

    声明: 1.本表格数据取自<日本の統計 2016>: 2.本表所有价格单位为人民币,其日元均以当年平均汇率兑换为此人民币价格: 3.其人民币—日元年均汇率数据取自IMF Data Exch ...

  9. 在ashx中使用Server对象

    Server.MapPath() System.Web.HttpContext.Current.Server.MapPath()

  10. IEnumerable<> ICollection <> IList<> 区别

    IEnumerable< ICollection < IList区别 public interface IEnumerable { IEnumerator GetEnumerator(); ...