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 ...
 
随机推荐
- 74LVC2G241双缓冲3态驱动器
 - Git之master ->! [rejected]   master (non-fast-forward)
			
出现这个情况可能是在克隆项目的时候强制关闭或者是在pull的时候强制关闭 运行命令:git pull --rebase origin master 然后就可以 git push origin mast ...
 - 2. UITest相关APIs
			
1. XCUIApplication 这是你正在测试的应用的代理.它能让你启动应用,这样你就能执行测试了.它每次都会新起一个进程,这会多花一些时间,但是能保证测试应用时的状态是干净的,这样你需要处理的 ...
 - hihocoder1079 离散化
			
思路:线段树 + 离散化. 测试用例: 3 10 1 10 1 3 6 10 实现: #include <bits/stdc++.h> using namespace std; typed ...
 - 一键修改android 字体和图片大小.
			
项目中需要动态更改 app的字体和图片, 在查阅中找到的更改主题的解决办法,和单独的修改字体的方法. 这两种方法的确有效果但是实现麻烦,在修改字体的过程中,找到一个额外的方法, 修改字体的实现更改 ...
 - EEPROM介绍
			
EEPROM( Electrically Erasable Programmable Read Only Memory )全称是电气可擦除可编程只读存储器,是非易失存储器,可以访问到每个字节,容量比较 ...
 - Idea导入tomcat源码
			
1.下载资源 下载主要包含两个包,已经编译的包和源码包,如图所示. 链接地址为: http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.93/bin ...
 - 回顾Spring MVC_01_概述_入门案例
			
SpringMVC: SpringMVC是Spring为展现层提供的基于MVC设计的优秀的Web框架,是目前最主流的MVC框架之一 SpringMVC通过注解,让POJO成为处理请求的控制器,而无须实 ...
 - pylint安装失败的解决方法
			
原文链接http://www.cnblogs.com/Loonger/p/7815335.html 使用命令pip3 install pylint安装pylint是出现错误.查了一圈也找不到答案.仔细 ...
 - (转)Spring+JDBC组合开发
			
http://blog.csdn.net/yerenyuan_pku/article/details/52882435 搭建和配置Spring与JDBC整合的环境 使用Spring+JDBC集成步骤如 ...