单链表和双链表的反转

题目

单链表的反转 链表初始为1,5,7,9,10,2,3 反转后 3,2,10,9,7,5,1;
双链表的反转 链表初始为1,5,7,9,10,2,3 反转后 3,2,10,9,7,5,1。

代码

/**
     * description 定义一个链表
     * param
     * return
     * author zhuyang
     * createTime 2021/11/21 15:39
     **/
    public static class Node{
        private int value;
        private Node next;
        public Node(int value){
            this.value=value;
        }

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

    /**
     * description 定义一个双链表
     * param
     * return
     * author zhuyang
     * createTime 2021/11/21 17:28
     **/
    public static class DoubleNode{
        private int value;
        private DoubleNode next;
        private DoubleNode last;
        public DoubleNode(int value){
            this.value=value;
        }
    }

    /**
     * description 双链表的反转
     * param [doubleNode]
     * return com.yxkj.algorithm.modular.primary.class04.ReverseList.DoubleNode
     * author zhuyang
     * createTime 2021/11/21 17:29
     **/
    public DoubleNode doubleNodeReverse(DoubleNode head){
        if (head==null){
            return null;
        }
        DoubleNode next=null;
        DoubleNode pre=null;
        while (head!=null){
            //定点下一个节点
            next=head.next;
            //将下一个节点的链接复制于当前节点的上个节点
            head.last=next;
            //将上一个节点赋予给当前节点的下一个节点
            head.next=pre;
            //当前节点作为下一个节点的上一个节点
            pre=head;
            //将下一个节点赋予给当前节点
            head=next;
        }
        return pre;

    }

    /**
     * description 进行链表的反转
     * param [node]
     * return com.yxkj.algorithm.modular.primary.class04.ReverseList.Node
     * author zhuyang
     * createTime 2021/11/21 15:42
     **/
    public Node reverse(Node head){
        if (head==null){
            return null;
        }
       Node pre=null;
       Node next=null;
       while (head!=null){
           //记录下一个指针位置
          next= head.next;
          //将head的下一个指针转向
          head.next=pre;
          //转向后将其赋给pre
          pre=head;
          //指向下一个位置
          head=next;
       }
      return pre;
    }

    /**
     * description  收集一个链表的集合元素
     * param [head]
     * return java.util.List[]
     * author zhuyang
     * createTime 2021/11/21 16:57
     **/
    public List<Integer> collect(Node head){
        List<Integer> list=new ArrayList<>();
        while (head!=null){
            list.add(head.value);
            head=head.next;
        }
        return list;
    }

    /**
     * description  收集一个链表的集合元素
     * param [head]
     * return java.util.List[]
     * author zhuyang
     * createTime 2021/11/21 16:57
     **/
    public List<Integer> collect(DoubleNode head){
        List<Integer> list=new ArrayList<>();
        while (head!=null){
            list.add(head.value);
            head=head.next;
        }
        return list;
    }

    /**
     * description 判断一个链表反转后是否成功
     * param [head]
     * return java.lang.Boolean
     * author zhuyang
     * createTime 2021/11/21 16:59
     **/
    public Boolean judge(Node head,List<Integer> initial){
        for (int i = initial.size()-1; i >=0; i--) {
            if (initial.get(i)!=head.value){
                return false;
            }
            head=head.next;
        }
        return true;
    }

    /**
     * description 随机产生一个链表
     * param [maxValue, length]
     * return com.yxkj.algorithm.modular.primary.class04.ReverseList.Node
     * author zhuyang
     * createTime 2021/11/21 17:04
     **/
    public Node generateRandomLinkedList(int maxValue,int length){
        Node node=null;
        Node head =null;
        for (int i = 0; i < length; i++) {
            if (head==null){
                head=new Node((int)(Math.random()*maxValue));
                node=head;
            }else {
                node.next=new Node((int)(Math.random()*maxValue));
                node=node.next;
            }
        }
        return head;
    }

    /**
     * description 核对双向链表的正确性
     * param [doubleNode]
     * return java.lang.Boolean
     * author zhuyang
     * createTime 2021/11/21 18:21
     **/
    public Boolean judgeDoubleNode(DoubleNode doubleNode,List<Integer> list){
        if (doubleNode==null){
            return false;
        }
        DoubleNode end = null;
        for (int i = list.size()-1; i >=0; i--) {
            if (list.get(i)!=doubleNode.value){
                return false;
            }
            end=doubleNode;
            doubleNode=doubleNode.next;
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i)!=end.value){
                return false;
            }
            end=end.last;
        }
        return true;
    }

    /**
     * description 产生一个双向链表节点 最大值为maxValue,长度为length
     * param [maxValue, length]
     * return com.yxkj.algorithm.modular.primary.class04.ReverseList.DoubleNode
     * author zhuyang
     * createTime 2021/11/21 17:58
     **/
    public DoubleNode generateRandomDoubleNode(int maxValue,int length){
        if (length==0){
            return null;
        }
        DoubleNode head=new DoubleNode((int)(Math.random()*maxValue));
        DoubleNode node=head;
        DoubleNode last=null;
        if (length==1){
            return head;
        }
        for (int i = 1; i < length; i++) {
            //对node下一个节点进行赋值
            node.next=new DoubleNode((int)(Math.random()*maxValue));
            //对node上一个节点进行赋值
            node.last=last;
            //将当前节点赋值给上一个节点作为下一次使用
            last=node;
            //进行节点转移
            node=node.next;

        }
        return head;
    }

    /**
     * description 对数器
     * param []
     * return void
     * author zhuyang
     * createTime 2021/11/21 17:02
     **/
    public void  comp(){
        int maxVlaue=100;
        int length=159;
        int testNuber=1000;
//        for (int i = 0; i < testNuber; i++) {
//            //产生一个链表
//            Node node = generateRandomLinkedList(maxVlaue, length);
//            //收集链表初始元素
//            List<Integer> collect = collect(node);
//            Node reverse = reverse(node);
//
//            if(!judge(reverse,collect)){
//                System.out.println("--------单链表反转失败-------");
//                System.out.println("单链表list初始集合为:"+collect);
//                System.out.println("单链表list反转后集合为:"+collect(reverse));
//                return;
//            }
//        }
//        System.out.println("--------单链表反转成功-------");

        for (int i = 0; i < testNuber; i++) {
            //产生一个双向链表
            DoubleNode node = generateRandomDoubleNode(maxVlaue, length);
            //收集链表初始元素
            List<Integer> collect = collect(node);
            //进行反转
            DoubleNode node1 = doubleNodeReverse(node);
            if (!judgeDoubleNode(node1,collect)){
                System.out.println("--------双链表反转失败-------");
                System.out.println("双链表list初始集合为:"+collect);
                System.out.println("双链表list反转后集合为:"+collect(node1));
                return;
            }
        }
        System.out.println("--------双链表反转成功-------");
    }

Gitee地址

https://gitee.com/zhuayng/foundation-study/blob/develop/JavaBasis/Algorithm/src/main/java/com/yxkj/algorithm/modular/primary/class04/ReverseList.java

XFS
原文地址:https://www.cnblogs.com/xiaofengshan/p/15733862.html