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 ...
随机推荐
- 获取span里面的值(特殊情况下 )
如何获取A? <div class="warpper"> <span class="content"> A <span>12 ...
- JavaScript alert()函数
avaScript alert()函数 alert--弹出消息对话框(对话框中有一个OK按钮) alert,中文"提醒"的意思 alert函数语法 alert(str); aler ...
- IT人怎样防止过劳死?如何成为时间的主人?
投行的朋友还没走几天,搜狐的一位同胞又去了.又是过劳死! 每当读到这类新闻,IT人无不反镜自照,顾影自怜.无法拼爹拼钱的我们,似乎只有拼命了.生活好惨淡啊! 有人说:年轻人,悠着点儿!立刻 ...
- poj3046
dp,可以再优化. #include <iostream> #include <cstdio> #include <cstring> using namespace ...
- Arduino中数据类型转换 int转换为char 亲测好使,itoa()函数
由于博主最近在做一个项目,需要采集不同传感器的数据,包括float型的HCHO,以及int型的PM2.5数据.但是最终向服务器上传的数据都得转换为char型才能发送,这是借鉴了一个github上面的实 ...
- iOS 循环引用解决方案
一.BLOCK 循环引用 一般表现为,某个类将block作为自己的属性变量,然后该类在block的方法体里面又使用了该类本身.构成循环引用. // 定义 block 的时候,会对外部变量做一次 cop ...
- 面向 AWS 专家的 Azure 云服务介绍
本文是面向 AWS 专家的 Azure 云服务介绍,参考本文可以帮助大家“按图索骥”在 Azure 的平台上找到能满足自己需求的服务. 公有云市场经过多年发展,已经涌现出几家大规模的提供商,如 Azu ...
- SQL 转换函数
1.字符串与字符串相加 字符串相加 得到的是拼接成一列的字符串类型 例如 select name+code from car name是nvarchar code也是nvarchar ...
- Google Colab调用cv2.imshow奔溃
当我在Google Colab运行如下代码 import cv2 import numpy as np image = cv2.imread('a.jpg') cv2.imshow('original ...
- git - GNU 交互工具
语法 git [options] [path1] [path2] gitps [options] gitview [options] filename 注意 GIT 包 的 主要 配置文件 是 .gi ...