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 ...
随机推荐
- Sublime Text 3列编辑
Sublime Text 3 的列编辑方式如下 1.使用鼠标 (Ubuntu 14.04验证通过) 不同的平台要使用不同的鼠标按钮: 1.1 OS X 鼠标左键 + Option 或: 鼠标中键 添加 ...
- 初识jstl标签库
JSTL(JSP Standard Tag Library,JSP标准标签库) 是一个不断完善的开源的 JSP 标签库,是由 apache 的 jakarta 小组来维护的,根据 JST L标签所提供 ...
- subprocess模块详解2
1.call() 和run功能类似,都是接受一个列表里的参数. >>> import subprocess >>> a = subprocess.call([&qu ...
- CF778B(round 402 div.2 E) Bitwise Formula
题意: Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied t ...
- JavaScript创建对象的七种方法
一. 工厂模式 创建: function createPerson(name,behavior){ var p=new Object(); p.name=name; p.behavior=behavi ...
- ES6学习笔记(10)----Set和Map数据结构
参考书<ECMAScript 6入门>http://es6.ruanyifeng.com/ Set和Map数据结构 1.Set 基本用法 Set是一种新的数据结构,它的成员都是唯一 ...
- Android studio 时间选择器
相当简单加载 gradle文件然后做一个textview即可. 1.首先我们要在build.gradle中写上这一行代码: compile 'com.feezu.liuli:timeselector: ...
- centos启用socks5服务
直接在终端用 root 安装 *** 官方客户端 apt-get install python-pip -ypip install shadowsocks 然后编辑 /etc/shadowsocks. ...
- function calling convention
这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=31 February 19, 2013 function calling c ...
- 在windows上安装Jenkins---tomcat流
在windows上安装Jenkins有两种方式: (1)jar流 在命令行中运行:java -jar jenkins.war 浏览器访问 localhost:8080,创建初始管理员帐号即可. (2) ...