Reverse the head list as group M: Example: Suppose m=3. List 1 - > 2 - > 3 - > 4 - > 5 - > 6-7-8 output > > : 3 - > 2 - > 1 - > 6 - > 5 - > 4-7 - > > 8Copy the code

You can solve this problem by using the stack structure. If the value in the stack is K, the stack will be ejected

Not practical stack structure, directly on the linked list to adjust

1. The code is as follows:

1.1 the Node. Java

package com.yuhl.right.node2.node;

/ * * *@author yuhl
 * @Date 2020/10/24 15:36
 * @Classname Node
 * @Description TODO
 */
public class Node {
    private int value;
    private Node next;

    public Node(a) {}public Node(int value, Node next) {
        this.value = value;
        this.next = next;
    }

    public int getValue(a) {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Node getNext(a) {
        return next;
    }

    public void setNext(Node next) {
        this.next = next; }}Copy the code

1.1 NoteTest. Java

package com.yuhl.right.node2.node;

import java.util.Stack;

/ * * *@author yuhl
 * @Date2020/10/24 insisting *@Classname NoteTest
 * @Description* * Reverse the head singly linked list as group m (not if it is less than m) : * Example: Assume m=3. List 1 - > 2 - > 3 - > 4 - > 5 - > 6-7-8 * > > output: 3 - > 2 - > 1 - > 6 - > 5 - > 4-7-8 * / > >
public class NoteTest {
    public static void main(String[] args) {
        // Build a singly linked list
        Node node8 = new Node(8.null);
        Node node7 = new Node(7, node8);
        Node node6 = new Node(6, node7);
        Node node5 = new Node(5, node6);
        Node node4 = new Node(4, node5);
        Node node3 = new Node(3, node4);
        Node node2 = new Node(2, node3);
        Node node1 = new Node(1, node2);
        Node nodeRev = getRevNode(node1,3);// Get the rotated list
        printLinck(nodeRev, 8);// Prints the rotated list
    }

    /** * use stack mechanism, time complexity O(n), control complexity O(1) *@paramHead Specifies the header *@paramK is a group of K *@returnNew header * */
    private static Node getRevNode(Node head, int K) {
        if (K < 2) {
            return head;
        }
        Node cur = head;
        Node start = null;
        Node pre = null;
        Node next = null;
        int count = 1;
        while(cur ! =null) {
            next = cur.getNext();
            if (count == K) {
                start = pre == null ? head : pre.getNext();
                head = pre == null ? cur : head;
                resign2(pre, start, cur, next);
                pre = start;
                count = 0;
            }
            count++;
            cur = next;
        }
        return head;
    }

    public static void resign2(Node left, Node start, Node end, Node right) {
        Node pre = start;
        Node cur = start.getNext();
        Node next = null;
        while(cur ! = right) { next = cur.getNext(); cur.setNext(pre); pre = cur; cur = next; }if(left ! =null) {
            left.setNext(end);
        }
        start.setNext(right);
    }

    private static void printLinck(Node nodeRev, int len) {
        Node noteTem = nodeRev;
        for (int i = 0; i < len; i++) {
            if (i == len - 1) {// the last node does not need to be followed ->
                System.out.print(noteTem.getValue());
            } else {
                System.out.print(noteTem.getValue() + "- >"); } noteTem = noteTem.getNext(); }}}Copy the code

2. The running result is as follows:

"C: \ Program Files \ Java \ jdk1.8.0 _201 \ bin \ Java exe"
3->2->1->6->5->4->7->8
Copy the code