LeetCode(25)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
分析
该题目核心是分组对一个链表进行反转,链接,如示例所示。
AC代码
/**
* 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) {
ListNode *p = head;
int len = 0;
while (p)
{
len++;
p = p->next;
}
if (len < k || k == 1)
return head;
vector<ListNode *> vl;
ListNode *h = head;
while (len >= k)
{
//找到len/k个子链表,分别翻转
ListNode *q = h;
for (int i = 1; i < k; i++)
q = q->next;
//保存后续结点
ListNode *r = q->next;
q->next = NULL;
vl.push_back(reverse(h));
h = r;
len -= k;
}//while
//将翻转的链表链接起来,保存剩余的小于k个的结点
ListNode *re = h;
if (vl.size() != 1)
{
for (int i = 0; i < vl.size() - 1; i++)
{
p = LastNode(vl[i]);
p->next = vl[i + 1];
}//for
p->next = vl[vl.size() - 1];
}
LastNode(vl[vl.size() - 1])->next = re;
head = vl[0];
return head;
}
ListNode *LastNode(ListNode *head)
{
while (head && head->next)
head = head->next;
return head;
}
ListNode* reverse(ListNode* head)
{
if (head == NULL || head->next == NULL)
return head;
ListNode *ret = head , *p = head->next;
ret->next = NULL;
while (p)
{
//保存下一个结点
ListNode *q = p->next;
p->next = ret;
ret = p;
p = q;
}
return ret;
}
};
LeetCode(25)Reverse Nodes in k-Group的更多相关文章
- Leetcode(25)- k个一组翻转链表
给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序. 示例 : 给定这个链表: ...
- [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(7)Reverse Integer
题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 分析: ...
- LeetCode(151) Reverse Words in a String
题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...
- LeetCode(25)-symmetric tree
题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...
- LeetCode(24) Swap Nodes in Pairs
题目 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...
- LeetCode(47)-Reverse Bits
题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...
- LeetCode(206) Reverse Linked List
题目 Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed eithe ...
- LeetCode(190) Reverse Bits
题目 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...
随机推荐
- 跟我一起玩Win32开发(15):ListView控件
这个控件其实不用阿拉来介绍,因为它太常见了,就好像我们一出门就会看到妹子一样常见.当然也可以说,它是对ListBox的扩充. 在使用该控件之前,我先介绍VS的一个相当好玩的功能. 在代码文件的#inc ...
- LightOj 1088 - Points in Segments (二分枚举)
题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1088 题目描述: 给出一个n位数升序排列的数列,然后q个查询,每个查询问指定 ...
- Hibernate Could not obtain transaction-synchronized Session for current thread问题处理
项目通过Hibernate查询时报出如下错误: Hibernate Could not obtain transaction-synchronized Session for current thre ...
- 宏 函数 内联函数inline
带参宏有时候可以代替函数作用:优点直接替代,省去函数调用过程的开销:但缺点也是很明显:容易出错,系统不做检查非常容易出错. 改进方案:内联函数:既有带参宏的直接替代(拷贝)的优点,又有系统检查的优点. ...
- 分区表,磁盘概念和parted的使用
分区表,磁盘概念和parted的使用 登录陌生系统首先要做的事: 个人认为,首先得知道Linux版本的什么:cat /etc/issue df:查看磁盘的分区和数据的分配情况,类型(NFS,ext4. ...
- RHEL 6.5---SVN服务实现过程
主机名 IP地址 master 192.168.30.130 slave 192.168.30.131 安装 [root@master ~]# yum install -y subversion h ...
- (转)Unity优化之减少Drawcall
转载:http://www.jianshu.com/p/061e67308e5f Unity GUI(uGUI)使用心得与性能总结 背景和目的 小哈接触Unity3D也有一段时间了,项目组在UI解决方 ...
- SonarQube+Svn+Jenkins环境搭建----问题总结
1.配置SVN后提示unable to access to repository,原因是使用的账户没有访问svn的权限,创建新的用户即可.注意新的用户,用户名,密码要跟svn上的权限一致. 创 ...
- UVa OJ 458
The Decoder Write a complete program that will correctly decode a set of characters into a valid m ...
- PMP项目管理学习笔记(7)——整合管理之指导和管理项目执行过程
过程剖析 输入:组织过程资产.企业环境要素.项目管理计划.批准的变更请求 工具:专家判断.项目管理信息系统 输出:工作绩效信息.可交付成果.变更请求.项目文档和计划更新 指导和管理项目执行过程包括: ...