【知识积累】单向链表和双向链表如何反转

一、单链表

package com.example.demo.algorithm.D002;

/**
 * @Description :
 * 单向链表
 *
 * @Author : Darren
 * @Date : 2021 年 02 月 08 日 21:53:06
 * @since : 1.0
 */
public class Node {

    public int value;

    public Node next;

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

二、双向链表

package com.example.demo.algorithm.D002;

/**
 * @Description :
 * 双向链表
 *
 * @Author : Darren
 * @Date : 2021 年 02 月 08 日 21:53:06
 * @since : 1.0
 */
public class DoubleNode {

    public int value;

    public DoubleNode last;

    public DoubleNode next;

    public DoubleNode(int value) {
        this.value = value;
    }
}

三、反转

package com.example.demo.algorithm.D002;

/**
 * @Description :
 * 单向链表和双向链表如何反转
 *
 * @Author : Darren
 * @Date : 2021 年 02 月 08 日 21:53:06
 * @since : 1.0
 */
public class J001_ReverseNode {

    public static void main(String[] args) {
        int len = 50;
        int value = 100;
        Node node = generateRandomNode(len, value);
        printNode(node);
        System.out.println();
        Node result1 = reverseNode(node);
        printNode(result1);

        System.out.println();

        DoubleNode doubleNode = generateRandomDoubleNode(len, value);
        printNode(doubleNode);
        System.out.println();
        DoubleNode result2 = reverseDoubleNode(doubleNode);
        printNode(result2);
    }

    public static Node reverseNode(Node head){
        if (head == null){
            return null;
        }

        Node pre = null;
        Node next = null;
        Node cur = head;

        //1 -> 2 -> 3 -> 4 -> 5
        //1、next=2   1.next=null  pre=1  head=2
        //2、next=3   2.next=1  pre=2 head=3
        //3、next=4   2.next=2  pre=3 head=4
        //3、next=5   2.next=3  pre=4 head=5
        while (cur != null){
            //下个节点给到next
            next = cur.next;

            //将当前节点的next指向前一个节点
            cur.next = pre;
            //将当前节点给到前一个节点
            pre = cur;

            //
            cur = next;
        }
        return pre;
    }

    public static DoubleNode reverseDoubleNode(DoubleNode head){
        if (head == null){
            return null;
        }

        DoubleNode pre = null;
        DoubleNode next = null;

        while (head != null){

            next = head.next;

            head.next = pre;
            head.last = next;
            pre = head;

            head = next;

        }
        return pre;
    }

    /**
     * 打印单向链表
     * @param node
     */
    public static void printNode(Node node){
        if (node == null){
            return;
        }
        System.out.print(node.value + " ");
        printNode(node.next);
    }

    /**
     * 打印双向链表
     * @param node
     */
    public static void printNode(DoubleNode node){
        if (node == null){
            return;
        }
        System.out.print(node.value + " ");
        printNode(node.next);
    }

    /**
     * 随机生成单向链表
     * @param len
     * @param value
     * @return
     */
    public static Node generateRandomNode(int len, int value){
        int size = (int) (Math.random() * (len + 1));
        if(size == 0){
            return null;
        }
        size--;
        Node head = new Node((int) (Math.random() * (value + 1)));
        Node pre = head;
        while (size != 0){
            Node cur = new Node((int) (Math.random() * (value + 1)));

            pre.next = cur;
            pre = cur;

            size--;
        }
        return head;
    }

    /**
     * 随机生成双向链表
     * @param len
     * @param value
     * @return
     */
    public static DoubleNode generateRandomDoubleNode(int len, int value){
        int size = (int) (Math.random() * (len + 1));
        if(size == 0){
            return null;
        }
        size--;
        DoubleNode head = new DoubleNode((int) (Math.random() * (value + 1)));
        DoubleNode pre = head;
        while (size != 0){
            DoubleNode cur = new DoubleNode((int) (Math.random() * (value + 1)));

            pre.next = cur;
            cur.last = pre;

            pre = cur;
            size--;
        }
        return head;
    }

}

 

Guess you like

Origin blog.csdn.net/axin1240101543/article/details/113846073