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

    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.

    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

     

    写一个子函数,专门做“从某个结点开始反转接下来k个结点,并传回下一组的前一结点”这个事情。 主函数中就不断回调这个方法,直到最后传回的是null即可。

    子函数中做三个事情。1.确认有没有k个非空结点可以操作,没有的话就不动直接传回null。 2.改变n1 ~ nk这k个节点的指针指到前面的结点去(四句顶真)。3.改变before 和n1这两个结点的next指针,完善头尾接龙。

    细节:1.主函数用dummy node,之后直接返回dummy.next即可,因为调用函数后是整个链表的结构就改变了,自动next也变了。 2.这种比较复杂的链表变动最好一开始在子函数前面用注释写好你的结构是怎么变动的,用于写的过程中指导。 

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    
    
    public class Solution {
        /*
         * @param head: a ListNode
         * @param k: An integer
         * @return: a ListNode
         */
        public ListNode reverseKGroup(ListNode head, int k) {
            // write your code here
            ListNode dummy = new ListNode(0);
            dummy.next = head;
    
            ListNode prev = dummy;
            while (prev != null) {
                prev = reverseOneGroup(prev, k);
            }
            return dummy.next;
        }
        
        // before -> n1 -> n2 -> ... -> nk-1 -> nk -> nk+1
        // =>
        // before -> nk -> nk-1 -> ... -> n2 -> n1 -> nk+1
        private ListNode reverseOneGroup(ListNode before, int k) {
            
            // check there are k nodes
            ListNode prev = before;
            for (int i = 0; i < k; i++) {
                prev = prev.next;
                if (prev == null) {
                    return null;
                }
            }
            
            // change n1 ~ nk's pointers
            prev = before;
            ListNode crt = before.next;
            for (int i = 0; i < k; i++) {
                ListNode temp = crt.next;
                crt.next = prev;
                prev = crt;
                crt = temp;
            }
            
            // before <-> n1 <- n2 <- ... <- nk-1 <- nk  nk+1
            // =>
            // before -> nk -> nk-1 -> ... -> n2 -> n1 -> nk+1 
            ListNode n1 = before.next;
            ListNode nk = prev;
            ListNode nkplus = crt;
            before.next = nk;
            n1.next = nkplus;
            
            return n1;
        }
    }
  • 相关阅读:
    ASP.NET使用SWFUpload上传大文件教学
    Gridview自定义分页(转载)
    扩展GridView之个性分页(转载)
    畅通工程
    一个人的旅行
    Stockbroker Grapevine
    Arbitrage
    More is better
    HDU Today
    Minimum Transport Cost
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/7858952.html
Copyright © 2011-2022 走看看