【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 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
/**
* 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的更多相关文章
- 【LeetCode】Reverse Nodes in k-Group(k个一组翻转链表)
这是LeetCode里的第25道题. 题目要求: 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最 ...
- 【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 ...
- 【leetcode】698. Partition to K Equal Sum Subsets
题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...
- [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 ...
- 【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 ...
- 【LeetCode】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【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 ...
- 【leetcode】Reverse Words in a String
今天第一次在leetcode上提交了一个题目,据说这个网站基本上都是名企面试笔试题,今天无意一进去就看到第一题居然就是昨天的腾讯实习生笔试题,赶紧注册了个账号做题. 题目描述: Given an in ...
- 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 算法思想:基本操作就是链表 ...
随机推荐
- JS网页加载进度条
参考:http://www.cnblogs.com/timy/archive/2011/12/07/2279200.html
- css+html 总结+归纳
趁着清明的3天时间,我专门看了xhtml的一套视频,虽然是xhtml但是也隐喻了很多经验,巩固了一下自己的基础,我自己也写了很多大大小小的东西,但是越写我越发觉自己基础的薄弱,果然没错!还是学到了一些 ...
- Java字节流:FilterInputStream FilterOutputStream
----------------------------------------------------------------------------------- FilterInputStrea ...
- Javaee----重新回顾servlet
最近面临找工作,不得不回顾一下java servelt . 发现lz的基本功还是很差 1. 每一个servlet都必须实现servlet接口,GenericServlet是个通用的.不特定于任何协议的 ...
- Android布局属性大全
RelativeLayout http://wenku.baidu.com/view/2e39724333687e21af45a97e.html?from=related&hasrec=1 h ...
- js中的换算小技巧
之前自己一直使用~~运算符来把‘112222’字符型的数值换算成整型的数值 但今天调试程序发现了一些问题 ~~'999'=>999 ~~'111111999'=>111111999 这些都 ...
- 清北暑假模拟day1 生活
/* 数字三角形,要求第K大的值,可以推知,如果得知k的范围,那么一定是在上一行可转移状态的对应范围内(反证法可以证明),这个在背包九讲里也有提及 */ #include<cstdio> ...
- 跨域攻击xss
要完全防止跨域攻击是很难的,对于有些站点是直接 拦截跨域的访问,在你从本站点跳转到其他站点时提醒,这算是一种手段吧. 而跨域攻击的发起就是在代码(包括html,css,js或是后台代码,数据库数据)里 ...
- 【C语言入门教程】5.3 函数的调用 与 参数
函数通过调用获得程序的控制权,函数的参数是调用者与函数的数据接口.函数可以定义一个或多个参数,也可以省略参数,调用时将与参数的数据类型相匹配的数据置于参数列表中,即可在函数体内使用.参数的调用有多种形 ...
- MVC中下拉列表绑定方法
方法一: 前端 @Html.DropDownListFor(a=>a.acate,ViewBag.CateList as IEnumerable<SelectListItem>) 后 ...