zoukankan      html  css  js  c++  java
  • Reverse Nodes in k-Group leetcode java

    题目:

    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)
    10      * 
    11      * 0->3->2->1->4->5->6
    12      *          |  |
    13      *          pre next
    14      * @param pre 
    15      * @param next
    16      * @return the reversed list's last node, which is the precedence of parameter next
    17      */
    18     private static ListNode reverse(ListNode pre, ListNode next){
    19         ListNode last = pre.next;//where first will be doomed "last"
    20         ListNode cur = last.next;
    21         while(cur != next){
    22             last.next = cur.next;
    23             cur.next = pre.next;
    24             pre.next = cur;
    25             cur = last.next;
    26         }
    27         return last;
    28     }
    29     
    30     public static ListNode reverseKGroup(ListNode head, int k) {
    31             if(head == null || k == 1)
    32                 return head;
    33                 
    34             ListNode dummy = new ListNode(0);
    35             dummy.next = head;
    36             int count = 0;
    37             ListNode pre = dummy;
    38             ListNode cur = head;
    39             while(cur != null){
    40                 count ++;
    41                 ListNode next = cur.next;
    42                 if(count == k){
    43                     pre = reverse(pre, next);
    44                     count = 0;   
    45                 }
    46                 cur = next;
    47             }
    48          return dummy.next;
    49         }

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

  • 相关阅读:
    艾伟:WCF从理论到实践(17):OO大背离(带视频+ppt+源码) 狼人:
    艾伟:MOSS 2007 项目的开发步骤 狼人:
    艾伟:WM有约(一):你好,CF 狼人:
    艾伟:WCF从理论到实践(13):事务投票 狼人:
    艾伟:WCF从理论到实践(15):响应变化 狼人:
    艾伟:Remember: 我们是做产品的,不是搞学术研究的 & 用事实说话,不要臆断 狼人:
    艾伟:WCF从理论到实践(10):异常处理 狼人:
    艾伟:WCF从理论到实践(16):操作重载(带视频+ppt+源码) 狼人:
    艾伟:WCF从理论到实践(12):事务 狼人:
    艾伟:[WCF的Binding模型]之五:绑定元素(Binding Element) 狼人:
  • 原文地址:https://www.cnblogs.com/springfor/p/3864530.html
Copyright © 2011-2022 走看看