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. ...
随机推荐
- ASM实例原始磁盘搜索路径
discovery diskstring==>ASM实例原始磁盘搜索路径,一般搜索/dev/raw/ /dev/oracleasm/ 初始化参数文件中为:asm_diskstring asmc ...
- Http请求的工具
1.火狐的插件 HttpRequester 安装方法:火狐浏览器的最右上角的菜单,打开附件组件 ,搜索:HttpRequester,重启火狐浏览器.在菜单栏的工具下可以看到 HttpRequester ...
- 禁止页面内按F5键进行刷新(扩展知识:禁止复制信息内容)
禁止页面内按F5键进行刷新: //禁止页面内按F5键进行刷新 function f_DisableF5Refresh(event) { var e = event || window.event; v ...
- 内存管理 初始化(七)kmem_cache_init_late 初始化slab分配器(下)
我们知道kmem_cache中对于每CPU都有一个array_cache,已作为每CPU申请内存的缓存. 此函数的目的在于:每个kmem_cache都有一个kmem_list3实例,该实例的shar ...
- Java学习之——泛型
1.概要 generics enable types (classes and interfaces) to be parameters when defining classes, interfac ...
- git恢复删除的分支及内容
git 删除分支git branch -D 分支名 git查看分支 git branch -a git 删除远程分支 git push origin :分支名 这里注意:git分支提交并且push了, ...
- level 6 - unit4 - 强调句
强调句 强调实义动词 范围: 一般现在时/一般过去式:肯定句 方法:v.前面加do/does/did 例子: i love you --> i do love you i loved you - ...
- 详解CorelDRAW中关于群组的操作
CorelDRAW软件中的“群组”功能键主要用于整合多个对象.在进行比较复杂的绘图编辑时,通常会有很多的图形对象,为了方便操作,可以对一些对象设定群组.设定群组以后的多个对象,将被看作一个单独的对象. ...
- 针对降质模型中的模糊SR
(PDF) Deep Plug-and-Play Super-Resolution for Arbitrary Blur Kernels https://www.researchgate.net/pu ...
- Spring学习总结六——SpringMVC一
一:什么是springMVC MVC,顾名思义,m就是model模型,包括处理业务.访问数据库以及封装数据实体类等,view视图层 负责和用户进行交互,就是展示给用户的部分,包括jsp页面,html等 ...