LeetCode 25 Reverse Nodes in k-Group Add to List (划分list为k组)
package leetcode_50; /***
*
* @author pengfei_zheng
* list划分为k组并且逆置
*/
public class Solution25 {
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public ListNode reverseKGroup(ListNode head, int k) {
ListNode curr = head;
int count = 0;
while (curr != null && count != k) { // find the k+1 node
curr = curr.next;
count++;
}
if (count == k) { // if k+1 node is found
curr = reverseKGroup(curr, k); // reverse list with k+1 node as head
// head - head-pointer to direct part,
// curr - head-pointer to reversed part;
while (count-- > 0) { // reverse current k-group:
ListNode tmp = head.next; // tmp - next head in direct part
head.next = curr; // preappending "direct" head to the reversed list
curr = head; // move head of reversed part to a new node
head = tmp; // move "direct" head to the next node in direct part
}
head = curr;
}
return head;
}
}
LeetCode 25 Reverse Nodes in k-Group Add to List (划分list为k组)的更多相关文章
- Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...
- [LeetCode] 25. 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. k ...
- [leetcode]25. 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. k ...
- [LeetCode]25. 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. k ...
- 蜗牛慢慢爬 LeetCode 25. Reverse Nodes in k-Group [Difficulty: Hard]
题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
- [leetcode 25]Reverse Nodes in k-Group
1 题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified li ...
- Java [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 li ...
- [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. k ...
- [Leetcode][Python]25: Reverse Nodes in k-Group
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Grouphttps://oj. ...
随机推荐
- CSDN极客头条使用指南
CSDN极客头条使用指南 今天给大家介绍一下CSDN博客最新推出的这个栏目--CSDN极客头条. 极客头条是什么 极客头条大家分享优质IT资源的聚集地. 大家不仅能够分享CSDN的文章,更能够将其它社 ...
- java多线程入门学习(一)
java多线程入门学习(一) 一.java多线程之前 进程:每一个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销.一个进程包括1--n个线程. 线程:同一类线程共享代码 ...
- js替换数组中的一个对象用for循环遍历
for(let i=0;i<statusList.length;i++){ if (statusList[i]['tableId'] === tableId) { statusList[i]=d ...
- highcharts学习1----Line charts
今天开始将之前使用的highcharts控件的经验进行总结和整理,一方面方便自己以后使用查询,同时也为正在学习的人们做一些指引,算是资源分享吧. 官网链接: http://www.highcharts ...
- Linux Shell 通配符、元字符、转义符使用实例介绍
https://www.cnblogs.com/chengmo/archive/2010/10/17/1853344.html
- Linux中什么是块设备 及 lsblk命令的使用
Linux中I/O设备分为两类:字符设备和块设备.两种设备本身没有严格限制,但是,基于不同的功能进行了分类.(1)字符设备:提供连续的数据流,应用程序可以顺序读取,通常不支持随机存取.相反,此类设备支 ...
- Header的Request部分和Response部分
转载:https://zh.wikipedia.org/wiki/HTTP%E5%A4%B4%E5%AD%97%E6%AE%B5#%E8%AF%B7%E6%B1%82%E5%AD%97%E6%AE%B ...
- [mysql] Incorrect string value: '\xE4\xBC\x9A\xE5\x91\x98' for column 'name' at row 1
数据库字符集错误, 修改为UTF8/utf8mb4字符集即可.
- SpringMVC-----使用Maven创建Web项目
1.创建一个Maven的project 2.不使用骨架,去掉勾 3.这里的Packing 选择 war的形式 由于packing是war包,那么下面也就多出了webapp的目录 4.由于我们的项目要使 ...
- SPREAD for Windows Forms 代码片段
'スクロールバーの移動 FpSpread1.ShowColumn(, , HorizontalPosition.Left) 'SetActiveCellの後.LeaveCellを呼び出す Dim ss ...