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 ...
随机推荐
- Ubuntu开机之后报错结局方法
sudo gedit /etc/default/apport 把里面的enabled=1改成enabled=,保存 201. 就是下雨也去.202. 我马上拿来.203. 孙英开飞机.204. 国华来 ...
- 跟我一起玩Win32开发(8):绘图(A)
从本篇开始,我就不吹牛皮,那就吹吹兔皮吧.说说与绘图有关的东东. 要进行绘制,首先要得到一个DC,啥是DC呢?按字面翻译叫设备上下文,也可以翻译为设备描述表,它主要指API为我们封装了一些与显示设备相 ...
- [WOJ1138]最大子序和
题目链接: WOJ1138 题目分析: 是很经典的一道题,乱搞的方法应该有不少,这里介绍O(n)的单调队列做法 首先维护一个前缀和,然后枚举每一个位置,维护一个前缀和单增的单调队列,但队列仅储存下标, ...
- Android课程设计第一天Android Studio安装
注意:课程设计只为完成任务,不做细节描述~ 学校有一个Android的课设,所以顺便把Android Studio安装了上去. 实际上安装过程并不复杂,只有几个地方需要注意~ 安装包可以去http:/ ...
- web 前端的一些问题
1. HTML 和 JS 一个网页显示出来的静态的内容为html创见的静态object 对这些object的操作通过JS来响应 2. HTTP cookie cookie是由server set, 由 ...
- 基于python的request库,模拟登录csdn博客
以前爬虫用urllib2来实现,也用过scrapy的爬虫框架,这次试试requests,刚开始用,用起来确实比urllib2好,封装的更好一些,使用起来简单方便很多. 安装requests库 ...
- 页面html图片按钮多种写法
原地址:http://blog.163.com/weison_hi/blog/static/17680404720118534033788/ 第一种: 在一般情况下按钮提交表单: <form i ...
- java访问数据库步骤详解
eg1: public static void main(String[] args) throws ClassNotFoundException, SQLException { //第一步:加载JD ...
- ssm框架搭建(下) 简单案例
前言 这段时间没有更新博客,一直想做一个基于ssm的简单的项目.经过多次的尝试,终于实现了简单的增删查改功能了. 正文 由于前端的技术不是很熟悉,经过多方的查阅,使用了bootstrap的样式,来使界 ...
- 洛谷 P1021 邮票面值设计
题目描述 给定一个信封,最多只允许粘贴N张邮票,计算在给定K(N+K≤15)种邮票的情况下(假定所有的邮票数量都足够),如何设计邮票的面值,能得到最大值MAX,使在1-MAX之间的每一个邮资值都能得到 ...