• 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 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

    题目给出一个单链表,以其中每K个结点为一组,逆置每一个组,并且只能使用常量的内存空间。

    第一种思路:原地逆置

    public class Solution {
        public ListNode reverseKGroup(ListNode head, int k) {
            if (head == null || head.next == null) {
                return head;
            }
    
            ListNode preGroupTail = null, currentGroupHead = head, nextGroupHead = null, current = head;
            for (int i=0; current != null; i++) {
                if (( (i+1) % k) == 0) {
                    if (i == k-1) {
                        head = current;
                    }
                    current = current.next;
                    nextGroupHead = current;
                    reverse(preGroupTail, currentGroupHead, nextGroupHead);
                    preGroupTail = currentGroupHead;
                    currentGroupHead = nextGroupHead;
                    continue;
                }
                current = current.next;
            }
            return head;
        }
        // 逆置K个节点
        public void reverse(ListNode preGroup, ListNode head, ListNode nextGroup) {
            ListNode pre = nextGroup;
            ListNode next = null;
            while (head != nextGroup) {
                next = head.next;
                head.next = pre;
                pre = head;
                head = next;
            }
            if (preGroup != null) {
                preGroup.next = pre;
            }
        }
    }



    第二种思路:使用数据结构的方法

    public class Solution {
        public ListNode reverseKGroup(ListNode head, int k) {
            if (head == null || head.next == null) {
                return head;
            }
            LinkedList<ListNode> list = new LinkedList<>();
            ListNode current = head, present = null;
            head = null;
            while (current != null) {
                int i = 0;
                for (; i<k && current!=null; i++) {
                    list.addFirst(current);   // 入栈
                    current = current.next;
                }
    
                if (i < k) {
                    if (head == null) {
                        head = list.removeLast();
                        present = head;
                    }
                    while (list.size() != 0) {
                        present.next = list.removeLast();
                        present = present.next;
                    }
                } else {
                    if (head == null) {
                        head = list.removeFirst();
                        present = head;
                        present.next = null;
                    }
                    while (list.size() != 0) {
                        present.next = list.removeFirst();
                        present = present.next;
                        present.next = null;
                    }
                }
            }
            return head;
        }
    }



    第三种思路:recursive(讨论区的解法,思路比较新奇优雅,只是递归方法占用的内存空间比较大)

    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode curr = head;
        int count = 0;
        while (curr != null && count != k) { // find the k+1 node
            curr = curr.next;
            count++;
        }
        if (count == k) { // if k+1 node is found
            curr = reverseKGroup(curr, k); // reverse list with k+1 node as head
            // head - head-pointer to direct part, 
            // curr - head-pointer to reversed part;
            while (count-- > 0) { // reverse current k-group: 
                ListNode tmp = head.next; // tmp - next head in direct part
                head.next = curr; // preappending "direct" head to the reversed list 
                curr = head; // move head of reversed part to a new node
                head = tmp; // move "direct" head to the next node in direct part
            }
            head = curr;
        }
        return head;
    }



    作业部落

  • 相关阅读:
    改动ScrollView的滑动速度和解决ScrollView与ViewPager的冲突
    SpringMVC 学习笔记(五) 基于RESTful的CRUD
    86/88汇编代码的执行调试
    HTML5+CSS3设计界面
    导入gradle项目
    Gradle安装 Gradle效率提升 eclipse安装gradle插件 【我】
    Eclipse集成Gradle 【Eclipse在线安装Gradle插件方法】
    gradle下载的依赖包位置 及 修改
    idea 普通 web项目配置启动【我】
    理解 IntelliJ IDEA 的项目配置和Web部署
  • 原文地址:https://www.cnblogs.com/read-the-spring-and-autumn-annals-in-night/p/12041932.html
Copyright © 2020-2023  润新知