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 ...
随机推荐
- spring boot :error querying database. Cause: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required
配置多个数据源启动报错,error querying database. Cause: java.lang.IllegalArgumentException: dataSource or dataSo ...
- div倾斜 文字不倾斜
- 洛谷 P2158 [SDOI2008]仪仗队 && 洛谷 P1447 [NOI2010]能量采集
https://www.luogu.org/problemnew/show/P2158 以人所在位置为(0,0)建立坐标系, 显然除了(0,1)和(1,0)外,可以只在坐标(x,y)的gcd(x,y) ...
- Altium Designer的一些功能
一 Snippets:将原理图或PCB的部分模块电路保存以便于以后重用.https://wenku.baidu.com/view/412a0dbcf121dd36a32d8217.html 二 设备制 ...
- HDU 1223 打表 + 大数
http://acm.hdu.edu.cn/showproblem.php?pid=1223 一般遇到这些题,我都是暴力输出前几项,找规律.未果. 然后输出n = 1时候,以A开始,有多少个答案, n ...
- Masonry自动布局与UIScrolView适配
Masonry介绍 Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简洁明了 并具有高可读性 而且同时支持 iOS 和 Max OS X.可以通过cocoa ...
- VS2013编译libjpeg库
第一步:找到刚刚解压出来的“jpeg-9a”文件夹下面的“makefile.vc”文件,用记事本或Notepad++等编辑工具打开,然后找到里面的“!include <win32.mak> ...
- 关于setTimeout和Promise执行顺序问题
先看一段代码 console.log('打印'+1); setTimeout(function(){ console.log('打印'+2); }) new Promise(function(reso ...
- Xilinx HLS
Xilinx 的高层次综合(High Level Synthesis, HLS)技术是将C/C++/SystemC软件语言转换成Verilog或VHDL硬件描述语言的技术.现已应用在SDAccel,S ...
- js生成随机的uuid
function generateUUID() { var d = new Date().getTime(); if(window.performance && typeof wind ...