题目:

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

题解:

这道题主要是利用reverse链表的方法,reverse的方法就是维护三个指针,然后别忘了保存next指针就行。

代码如下:

 1     //http://www.cnblogs.com/lichen782/p/leetcode_Reverse_Nodes_in_kGroup.html
 2     /**
 3      * Reverse a link list between pre and next exclusively
 4      * an example:
 5      * a linked list:
 6      * 0->1->2->3->4->5->6
 7      * |           |   
 8      * pre        next
 9      * after call pre = reverse(pre, next)
      * 
      * 0->3->2->1->4->5->6
      *          |  |
      *          pre next
      * @param pre 
      * @param next
      * @return the reversed list's last node, which is the precedence of parameter next
      */
     private static ListNode reverse(ListNode pre, ListNode next){
         ListNode last = pre.next;//where first will be doomed "last"
         ListNode cur = last.next;
         while(cur != next){
             last.next = cur.next;
             cur.next = pre.next;
             pre.next = cur;
             cur = last.next;
         }
         return last;
     }
     
     public static ListNode reverseKGroup(ListNode head, int k) {
             if(head == null || k == 1)
                 return head;
                 
             ListNode dummy = new ListNode(0);
             dummy.next = head;
             int count = 0;
             ListNode pre = dummy;
             ListNode cur = head;
             while(cur != null){
                 count ++;
                 ListNode next = cur.next;
                 if(count == k){
                     pre = reverse(pre, next);
                     count = 0;   
                 }
                 cur = next;
             }
          return dummy.next;
         }

Reference:http://codeganker.blogspot.com/2014/02/reverse-nodes-in-k-group-leetcode.html

Reverse Nodes in k-Group leetcode java的更多相关文章

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

  2. Reverse Nodes In K Group,将链表每k个元素为一组进行反转---特例Swap Nodes in Pairs,成对儿反转

    问题描述:1->2->3->4,假设k=2进行反转,得到2->1->4->3:k=3进行反转,得到3->2->1->4 算法思想:基本操作就是链表 ...

  3. Reverse Words in a String leetcode java

    题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is ...

  4. 【Reverse Nodes in k-Group】cpp

    题目: 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个结点进行链表反转(链表)

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

  6. 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划分 ...

  7. LeetCode 笔记系列六 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. ...

  8. LeetCode: Reverse Nodes in k-Group 解题报告

    Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...

  9. Leetcode 25/24 - Reverse Nodes in k-Group

    题目描述 Leetcode 24 题主要考察的链表的反转,而 25 题是 24 的拓展版,加上对递归的考察. 对题目做一下概述: 提供一个链表,给定一个正整数 k, 每 k 个节点一组进行翻转,最后返 ...

  10. [LintCode] 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 ...

随机推荐

  1. 关于mysql中storage_engine中 MYISAM 和 INNODB 的选择

    简单点说 读操作多用myisam 写操作多用innodb 不过现在大家好像基本都用innodb,本人小白一个就直接用InnoDB. MySQL自20多年前成立以来一直支持可插拔存储引擎,但在一段相当长 ...

  2. 使用Mongo索引需要注意的几个点

    1.正则表达式和取反运算符不适合建立索引 正则表达式:$regex 取反运算符:$ne ,$nin 2.backgroud建立索引速度缓慢 前台创建是会有阻塞,backgroud效率缓慢,实际情况实际 ...

  3. 1025 PAT Ranking (25)(25 point(s))

    problem Programming Ability Test (PAT) is organized by the College of Computer Science and Technolog ...

  4. python开发_imghdr_图像格式支持

    在python中,imghdr模块对图像格式提供了支持 该模块主要是处理识别图像的格式 imghdr模块提供的函数如下: imghdr.what(filename, h=None) Tests the ...

  5. HDU 1692 Destroy the Well of Life 水题

    Destroy the Well of Life Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showprob ...

  6. Alpha冲刺(1/10)——追光的人

    1.队友信息 队员学号 队员博客 221600219 小墨 https://www.cnblogs.com/hengyumo/ 221600240 真·大能猫 https://www.cnblogs. ...

  7. spring对事务支持的三种形式

    spring对事务支持的三种形式: 1.通过spring配置文件进行切面配置 <bean id="***Manager" class="org.springfram ...

  8. Namespace declaration statement has to be the very first statement in the script

    php 中 Namespace declaration statement has to be the very first statement in the script 错误解决方法: 在PHP文 ...

  9. max_binlog_cache_size

    http://blog.mimvp.com/2017/07/mysql-yi-ge-can-shu-yin-qi-de-dang-ji-xue-an/ max_binlog_cache_size 表示 ...

  10. spring mvc 下 applicationContext 和webApplicationContext

    spring中的ApplicationContexts可以被限制在不同的作用域.在web框架中,每个DispatcherServlet有它自己的WebApplicationContext,它包含了Di ...