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. java javacv调用摄像头并拍照

    调用摄像头并拍张照片,我一开始用的java的jmf媒体框架,但这个有很多的局限性不好使并且很有麻烦,兜了一圈发现javacv东西,研究之后这东西简单,方便:废话不多说了来重点. javacv官网:点击 ...

  2. 过滤Xss

    /** * 防xss过滤 * * @author rentingshuang <tingshuang@rrkd.cn> * @param type $string * @param typ ...

  3. AngularJs自定义指令详解(6) - controller、require

    在前面文章中提到一旦声明了require,则链接函数具有第四个参数:controller. 可见require和controller是配合使用的. 在自定义指令中使用controller,目的往往是要 ...

  4. DISK 100% BUSY,谁造成的?

    iostat等命令看到的是系统级的统计,如果要追查是哪个进程导致的I/O繁忙,应该怎么办? iostat等命令看到的是系统级的统计,比如下例中我们看到/dev/sdb很忙,如果要追查是哪个进程导致的I ...

  5. Spring RestTemplate: 比httpClient更优雅的Restful URL访问, java HttpPost with header

    { "Author": "tomcat and jerry", "url":"http://www.cnblogs.com/tom ...

  6. ue4 FPaths各目录

    GetWrappedLaunchDir() 启动时的工作目录,因为马上要把工作目录改为下面所说的exe所在目录,所以会先把当前的缓存起来 FPlatformProcess::BaseDir() 这个是 ...

  7. (python)图片处理Pillow模块的使用

    Pillow中最重要的类就是Image,该类存在于同名的模块中.可以通过以下几种方式实例化:从文件中读取图片,处理其他图片得到,或者直接创建一个图片. 还有一个类为ImageDraw,用来画图. 1. ...

  8. VS与ultraedit 正则表达式替换

    ASP中把request("{param}")调用替换为requestX("{param}") VS 表达式替换(?<a>request\(&quo ...

  9. HTML的ul和li标签的使用

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  10. (转)EntityFramework之领域驱动设计实践

    EntityFramework之领域驱动设计实践 - 前言 EntityFramework之领域驱动设计实践 (一):从DataTable到EntityObject EntityFramework之领 ...