题目

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;
}
};

GitHub测试程序源码

LeetCode(25)Reverse Nodes in k-Group的更多相关文章

  1. Leetcode(25)- k个一组翻转链表

    给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序. 示例 : 给定这个链表: ...

  2. [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 ...

  3. LeetCode(7)Reverse Integer

    题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 分析: ...

  4. 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 ...

  5. LeetCode(25)-symmetric tree

    题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...

  6. LeetCode(24) Swap Nodes in Pairs

    题目 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...

  7. LeetCode(47)-Reverse Bits

    题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...

  8. LeetCode(206) Reverse Linked List

    题目 Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed eithe ...

  9. LeetCode(190) Reverse Bits

    题目 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...

随机推荐

  1. sql mysql和sqlserver存在就更新,不存在就插入的写法(转)

    转自:http://hi.baidu.com/tidy0608/item/ff930fe2436f2601560f1dd9 sqlsever数据存在就更新,不存在就插入的两种方法 两种经常使用的方法: ...

  2. 使用Ctex中遇到的一些问题

    一般下载好Ctex,我是使用Latex+dvi2pdf完成编译的,但是发现推荐的使用为:1)运行CCT & Latex命令生成两次dvi和ps文件 2)使用dvi2pdf编译dvi文件生成pd ...

  3. Suricata的总体架构

    Suricata的总体架构  报文检测系统通常四大部分,报文获取.报文解码.报文检测.日志记录:suricata不同的功能安装模块划分,一个模块的输出是另一个模块的输入,suricata通过线程将模块 ...

  4. spark序列化及MapOutputTracker解析

    本文主要打算对spark内部的序列化机制以及在shuffle map中起衔接作用的MapOutputTracker做一下剖析.主要涉及具体实现原理以及宏观设计的一些思路. 1,spark序列化 任何一 ...

  5. zoj3768Continuous Login

    链接 这题通过暴力可以看出最多不超过3 具体为什么..等着看大牛的题解. 可以预处理出来两个数之和 用bool存下 然后枚举一个数 二分剩余数的位置就可以了 勉强可过 #include <ios ...

  6. state vs props

    我们来一个关于 state 和 props 的总结. state 的主要作用是用于组件保存.控制.修改自己的可变状态.state 在组件内部初始化,可以被组件自身修改,而外部不能访问也不能修改.你可以 ...

  7. C#: static关键字的作用(转)

    C#: static关键字的作用   static意思是静态,可以修饰类.字段.属性.方法 标记为static的就不用创建实例对象调用了,可以通过类名直接点出来 static三种用法: 1.用于变量前 ...

  8. html到计时特效(直接代码)

    <!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/h ...

  9. Google的Java编程风格指南

    作者:Hawstein出处:http://hawstein.com/posts/google-java-style.html声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Cre ...

  10. AlertDialog的几种用法

    xml代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro ...