• 单链表反转


    单链表反转

    package com.etime;
    
    /**
     * @author Nian
     * @Date 2022/11/21 20:19
     * @description
     * @Note
     */
    public class ReverseLinkedList {
        public static void main(String[] args) {
            Node head = new Node();
            head.setData(1);
            Node node2 = new Node();
            node2.setData(2);
            head.setNext(node2);
            Node node3 = new Node();
            node3.setData(3);
            node2.setNext(node3);
            System.out.println(head);
            Node revers = revers(head);
            System.out.println(revers);
        }
    
        /**
         * 反转方法
         * @param head  头结点
         * @return Node
         * @author Nian
         * @Date 2022/11/21 21:48
        **/
        public static Node revers(Node head){
            Node currentNode = head;
            Node currentNext = null;
            if(currentNode.hasNext()){
                currentNext = head.getNext();
            }else {
                return currentNode;
            }
            while (currentNode.hasNext()){
                Node temp = null;
                //修改指针
                if(currentNext.hasNext()){
                   temp = currentNext.getNext();
                }else {
                    break;
                }
                head.setNext(null);
                currentNext.setNext(currentNode);
                //标记后移
                currentNode = currentNext;
                currentNext = temp;
            }
            //最后一个节点还未指向之前的链表,while循环之后修改最后一个节点的next节点
            currentNext.setNext(currentNode);
            return currentNext;
        }
    
        //节点
        static class Node{
            private Integer data;
            private Node next;
    
            public Node() {
            }
    
            public Node(Integer data, Node next) {
                this.data = data;
                this.next = next;
            }
    
            public void setData(Integer data) {
                this.data = data;
            }
    
            public void setNext(Node next) {
                this.next = next;
            }
    
            public Integer getData(){
                return this.data;
            }
    
            public Node getNext() {
                return next;
            }
    
            public boolean hasNext(){
                return next != null;
            }
    
            @Override
            public String toString() {
                if(this.hasNext()){
                    return this.data.toString() + "," +this.next.toString();
                }else {
                    return this.data.toString();
                }
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
  • 相关阅读:
    1044 Shopping in Mars(二分)
    如何编写一个Systemd Service
    微信小程序 — WeUI slideview 左滑删除(二)
    目标检测YOLO实战应用案例100讲-面向恶劣环境下的多模态 行人识别
    UMA 2 - Unity Multipurpose Avatar☀️八.UMA内置实用Recipes插件
    散列表(1)-集合/用位向量实现集合
    Z-DArg-GR-pNA,113711-77-6
    计算机网络——数据链路层の选择题整理
    LamdaUpdateWapper失效问题
    HashMap为什么线程不安全?
  • 原文地址:https://blog.csdn.net/m0_53658634/article/details/127973559