1 题目:

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

2 思路:

我自己想出来的。使用递归,每次只处理k个,递归处理剩下的。

3 代码:

    public ListNode reverseKGroup(ListNode head, int k) {
if(head == null){
return null;
} ListNode fake=new ListNode(0);
fake.next = head;
ListNode l = head;
int count = 0;
while(l != null){
count++;
l = l.next;
}
if(k>count) return head; ListNode after = null;
l = head;
ListNode pre = l.next;
for(int i=0; i<k ; i++){
fake.next = l;
l.next = after;
after = l;
l = pre;
if(pre != null) pre = pre.next;
}
head.next=reverseKGroup(l,k); return fake.next;
}

[leetcode 25]Reverse Nodes in k-Group的更多相关文章

  1. Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)

    Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...

  2. LeetCode 25 Reverse Nodes in k-Group Add to List (划分list为k组)

    题目链接: https://leetcode.com/problems/reverse-nodes-in-k-group/?tab=Description   Problem :将一个有序list划分 ...

  3. [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 ...

  4. 蜗牛慢慢爬 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. ...

  5. [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  ...

  6. [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  ...

  7. [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 ...

  8. 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 ...

  9. [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  ...

随机推荐

  1. Python:线程

    Python中创建线程有两种方式:函数或者用类来创建线程对象. 函数式:调用 _thread 模块中的start_new_thread()函数来产生新线程. 类:创建threading.Thread的 ...

  2. jQuery简介

    jQuery简介 jQuery是继Prototype之后的又一个javascript库,它由John Resig创建于2006年1月. Javascript库作用比较: 1. Prototype(ht ...

  3. angular js 自定义js错误处理(Angularjs js error handler)

    使用AngularJS的时候,对JS错误如何自定义处理?(比如用Google Analytics记录angularjs使用中出现的js错误) AngularJS自带一个错误处理service:$exc ...

  4. DOS:将某文件夹下面的所有某一类型文件名输出

    C:\Users\lv>cd /d C:\Siemens\Teamcenter11\lib C:\Siemens\Teamcenter11\lib>dir /B *.lib >lis ...

  5. 时间“Thu Aug 14 2014 14:28:06 GMT+0800”的转换

    var date = "Thu Aug 14 2014 14:28:06 GMT+0800"; var va = DateTime.ParseExact(date, "d ...

  6. 移动平台对于META标签的定义[转]

    下面介绍一些有关标记的例子及解释. 一.meta 标签分两大部分:HTTP 标题信息(http-equiv)和页面描述信息(name). 1.http-equiv 属性的 Content-Type 值 ...

  7. C# 加密

    一.RSA加密解密 using System; using System.Collections.Generic; using System.IO; using System.Linq; using ...

  8. 【计算几何】bzoj1043 [HAOI2008]下落的圆盘

    n^2枚举圆盘,用两圆圆心的向量的极角+余弦定理求某个圆覆盖了该圆的哪一段区间(用弧度表示),最后求个区间并. 注意--精度--最好再累计区间的时候,把每个区间的长度减去EPS,防止最后覆盖的总区间超 ...

  9. XAF ListView 移除顶部工具栏

    此方法适用于C/S及B/S,无需分别写在web和win中. Module下新建ViewController,代码如下: public partial class GongZuoJiaoShen_Yin ...

  10. Mysql 分区处理NULL的得方式

    MySQL分区处理NULL值得方式 一般情况下,MySQL的分区把NULL当做零值,或者一个最小值进行处理 对于range分区 create table test_null( id int ) par ...