题目:

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. maven 发布jar包到远程仓库

    有的时候我们需要发布一些自己写的相关jar包到maven私服,供项目组使用. 首先在setting.xml文件添加,这里 注意 要保证该账户有发布的权限 <servers> <ser ...

  2. Linux命令学习<不断更新>

    没有系统的学习过Linux命令,遇到了就学习一下,慢慢积累. 1.echo 命令,学习网站『https://linux.cn/article-3948-1.html』. echo单词有回声.共鸣的意思 ...

  3. apt-get出现无法定位安装包问题解决

    这个问题出现在sources.list上 编辑/etc/apt/sources.list下的文件 找到检查你的存储库是否正确 你可以在以下页面找到不同版本 Kali Linux 的存储库:http:/ ...

  4. [COGS2479]偏序

    [COGS2479]偏序 题目大意: \(n(n\le50000)\)个四元组,求四维偏序. 思路: CDQ分治套CDQ分治套树状数组. 细节: 第二层CDQ之前要备份数组\(a\),否则第二层CDQ ...

  5. XMOJ 1133: 膜拜大牛 计算几何/两圆相交

    1133: 膜拜大牛 Time Limit: 1 Sec  Memory Limit: 131072KiBSubmit: 9619  Solved: 3287 题目连接 http://acm.xmu. ...

  6. SGU 403 Game with points

    408. Game with points Time limit per test: 0.25 second(s)Memory limit: 65536 kilobytes input: standa ...

  7. 读书笔记_Effective_C++_条款三十五:考虑virtual函数以外的其他选择

    举书上的例子,考虑一个virtual函数的应用实例: class GameCharacter { private: int BaseHealth; public: virtual int GetHea ...

  8. UEFI引导模式

    Author: JinDate: 20140827System: windows 刚帮楼下的公司解决了个问题. 原来的办公电脑,预装linux,他们重装成win7.新买的电脑预装成win8,安装出问题 ...

  9. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 (动态树入门)

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1528  Solved: 644[Submit][ ...

  10. cocos2d-x3.0 RichText

    .h #include "cocos2d.h" #include "cocos-ext.h" #include "ui/CocosGUI.h" ...