算法003:将单链表的每K个节点之间逆序,例子:假设m=3. 链表 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 ->8 output: 3 -> 2 -> 1 ->6 -.....

293 阅读1分钟
题目:
将head单链表以m为组反转链表(不足m则不反转):
例子:假设m=3. 链表 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 ->8
output: 3 -> 2 -> 1 ->6 ->5 ->4 -> 7 ->8

思路:
可以使用栈的结构解决这个问题,如果栈中值为K则弹出栈

不实用栈的结构,直接在链表上进行调整

1.代码如下:

1.1Node.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() {
    }

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

    public int getValue() {
        return value;
    }

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

    public Node getNext() {
        return next;
    }

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

1.1NoteTest.java

package com.yuhl.right.node2.node;

import java.util.Stack;

/**
 * @author yuhl
 * @Date 2020/10/24 15:38
 * @Classname NoteTest
 * @Description
 *
 * 将head单链表以m为组反转链表(不足m则不反转):
 * 例子:假设m=3. 链表 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) {
        //构建单链表
        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);//获取旋转之后的链表
        printLinck(nodeRev, 8);//打印旋转之后的链表
    }

    /**
     * 使用栈的机构,时间复杂度O(n),控件复杂度为O(1)
     * @param head 头结点
     * @param K 每K个为一组
     * @return 新的头结点
     *
     */
    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) {//最后的一个结点后面无须->
                System.out.print(noteTem.getValue());
            } else {
                System.out.print(noteTem.getValue() + "->");

            }
            noteTem = noteTem.getNext();
        }
    }
}

2.运行结果如下:

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