Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of
class Solution {
public:
ListNode *reverseKGroup(ListNode *head, int k) {
if (!head || !(head->next) || k < 2)
return head;
// count k nodes
ListNode *nextgp = head;
for (int i = 0; i < k; i++)
if (nextgp)
nextgp = nextgp->next;
else
return head;
// reverse
ListNode *prev = NULL, *cur = head, *next = NULL;
while (cur != nextgp) {
next = cur->next;
if (prev)
cur->next = prev;
else
cur->next = reverseKGroup(nextgp, k);
prev = cur;
cur = next;
}
return prev;
}
};
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of的更多相关文章
- 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.
我用String代替了链表显示,本题的大意是每k个进行逆序处理,剩下的不够k个的就按照原顺序保留下来. public class ReverseNodes { public static void m ...
- [Linked List]Reverse Nodes in k-Group
Total Accepted: 48614 Total Submissions: 185356 Difficulty: Hard Given a linked list, reverse the no ...
- 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List
9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...
- HIVE出现Read past end of RLE integer from compressed stream Stream for column 1 kind LENGTH position: 359 length: 359 range: 0错误
错误日志 Diagnostic Messages for this Task: Error: java.io.IOException: java.io.IOException: java.io.EOF ...
- [Linked List]Reverse Linked List,Reverse Linked List II
一.Reverse Linked List (M) Reverse Linked List II (M) Binary Tree Upside Down (E) Palindrome Linked ...
- LeetCode之“链表”:Reverse Linked List && Reverse Linked List II
1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...
- *Amazon problem: 234. Palindrome Linked List (reverse the linked list with n time)
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- Data Structure Linked List: Reverse a Linked List in groups of given size
http://www.geeksforgeeks.org/reverse-a-list-in-groups-of-given-size/ #include <iostream> #incl ...
- 863. All Nodes Distance K in Binary Tree 到制定节点距离为k的节点
[抄题]: We are given a binary tree (with root node root), a target node, and an integer value K. Retur ...
随机推荐
- asp.net 图表
感谢csdn深南大道,文章转自http://blog.csdn.net/smartsmile2012/article/details/17356673 前台代码 <div> <asp ...
- Objective-C Log Handling
NSLog method In order to print logs, we use the NSLog method in Objective-C programming language whi ...
- linux下phpstudy的搭建以及网站的搭建
step1: 下载SSH连接到IP地址(阿里云上购买的) step2: 安装下载phpstudywget -c http://lamp.phpstudy.net/phpstudy.bin chmod ...
- mysqldumpslow及explain使用简介
- 总结SQL Server窗口函数的简单使用
总结SQL Server窗口函数的简单使用 前言:我一直十分喜欢使用SQL Server2005/2008的窗口函数,排名函数ROW_NUMBER()尤甚.今天晚上我在查看SQL Server开发的相 ...
- hihoCode r#1077 : RMQ问题再临-线段树
思路: 两种实现方法: (1)用链表(2)用数组. #include <bits/stdc++.h> using namespace std; int n, q, L, R, op, P, ...
- ABAP Netweaver, SAP Cloud Platform和Kubernetes的用户区分
ABAP Dialog: Individual, interactive system access. System: Background processing and communication ...
- codeforce Gym 100203I I WIN (网络流)
把'I'拆成容量为1一条边,一个入点一个出点,入点和相邻的'W'连一条容量为1的边,出点和相邻的'N'连一条容量为1,所有的'W'和源点连一条容量为1边,所有的'N'和汇点连一条容量为1的边,表示只能 ...
- visual c++ build tools的安装与使用
https://visualstudio.microsoft.com/zh-hans/thank-you-downloading-visual-studio/?sku=BuildTools&r ...
- Greenplum介绍-table
GP中的table和其它关系型数据表是一样的,除了数据被分布在不同的segment以外. 建表时需定义以下几个方面:1. 指定列和数据类型2. 约束3. 分布策略4. 数据存储方式5. 大表分区策略 ...