• 数据结构与算法-从尾到头打印单链表(百度面试题)



    从尾到头打印单链表-百度面试题

    方式一:

    先将单链表翻转然后再链表
    单链表的翻转见单链表的翻转
    (这样做会破坏原来的单链表的一个结构【不建议这么操作】)

    方式二:

    可以利用栈这个数据结构,将各个节点压入栈中,然后利用栈的先进后出的特点就实现了逆序打印的效果
    栈的演示:

    package com.chad.lambda;
    
    import java.util.Stack;
    
    public class TestStack {
        public static void main(String[] args) {
            //演示栈的基本使用
            Stack<String> stack = new Stack<String>();
    
            //入栈
            stack.add("jack");
            stack.add("tom");
            stack.add("chad");
            stack.add("bunny");
    
            //出栈
            while(stack.size() > 0){
                System.out.println(stack.pop());
            }
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    运行结果:

    在这里插入图片描述

    我们发现出栈的数据顺序和进栈的顺序正好是相反的

    使用方式二来实现逆序打印

    编写方法reversePrint()

    //使用方式二实现逆序打印
    public  static void reversePrint(HeroNode head){
        if(head.next == null){
            return;//空链表不用打印
        }
        Stack<HeroNode> stack = new Stack<>();
        HeroNode cur = head.next;
        while (cur != null){
            stack.push(cur);
            cur = cur.next;
        }
    
        while (stack.size() > 0){
    
            System.out.println(stack.pop());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在main方法中添加

    //显示一把
    singleLinkedList.list();
    System.out.println(“翻转打印”);
    reversePrint(singleLinkedList.getHead());

    运行结果

    在这里插入图片描述

    至此实现了单链表的逆序打印

    全部代码

    package com.chad.lambda;
    
    import java.util.Stack;
    
    public class SingleLinkedListDemo {
        public static void main(String[] args) {
            //进行测试
            HeroNode hero1 = new HeroNode(1,"宋江","及时雨");
            HeroNode hero2 = new HeroNode(2,"卢俊义","玉麒麟");
            HeroNode hero3 = new HeroNode(3,"吴用","智多星");
            HeroNode hero4 = new HeroNode(4,"林冲","豹子头");
    
            SingleLinkedList singleLinkedList = new SingleLinkedList();
            //加入英雄
            singleLinkedList.addByOrder(hero1);
            singleLinkedList.addByOrder(hero4);
            singleLinkedList.addByOrder(hero2);
            singleLinkedList.addByOrder(hero3);
    
    
            //显示一把
            singleLinkedList.list();
            System.out.println("翻转打印");
            reversePrint(singleLinkedList.getHead());
        }
    
        //使用方式二实现逆序打印
        public  static void reversePrint(HeroNode head){
            if(head.next == null){
                return;//空链表不用打印
            }
            Stack<HeroNode> stack = new Stack<>();
            HeroNode cur = head.next;
            while (cur != null){
                stack.push(cur);
                cur = cur.next;
            }
    
            while (stack.size() > 0){
    
                System.out.println(stack.pop());
            }
    
        }
    
        //Tencent Interview Questions
        public static void reverseList(HeroNode head){
            //if LinkedList is null or just one node return
            if( head.next == null || head.next.next == null){
                return;
            }
            HeroNode cur = head.next;
            HeroNode next = null;
            HeroNode reverseHead = new HeroNode(0,"","");
    
            //遍历原来的列表
            while(cur != null){
                next = cur.next;
                cur.next = reverseHead.next;
                reverseHead.next = cur;
                cur = next;
    
            }
            head.next = reverseHead.next;
    
    
        }
    
        //Sina Interview Questions
        public static HeroNode findLastNode(HeroNode head, int k){
            if(  head.next == null){
                System.out.println("linkedList is Empty");
                return null;
            }
            int length = getLength(head);
            if(k<=0 || k> length){
                System.out.println("The input number is not right");
                return null;
            }
            //Define an auxiliary variable
            HeroNode cur = head.next;
            for (int i = 0; i < length - k; i++) {
                cur = cur.next;
            }
            return cur;
        }
    
        //get singleLinkedList's node number(not head node)
        public static int getLength(HeroNode head){
            if (head.next == null){
                System.out.println("The LinkList is Empty");
                return 0;
            }
            int length = 0;
            //定义一个辅助变量
            HeroNode cur = head.next;
            while(cur != null){
                length++;
                cur = cur.next;
            }
            return length;
        }
    
    }
    
    //定义SingleLinkedList管理我们的英雄
    class SingleLinkedList{
        //初始化一个头节点,不存放具体数据
        private HeroNode head = new HeroNode(0,"","");
    
        public HeroNode getHead() {
            return head;
        }
    
    
    
        //不考虑顺序,找到链表的最后节点,将最后的next指向新的节点
        public void addHero(HeroNode heroNode){
            HeroNode temp = head;
            //遍历链表找到最后一个
            while(true){
                if(temp.next == null){
                    break;
                }
                temp = temp.next;
            }
    
            temp.next = heroNode;
        }
    
        //添加方法二
        public void addByOrder(HeroNode heroNode){
            //因为头节点不能动
            HeroNode temp = head;
            //因为是处于添加到的节点上
            boolean flag = false;//添加的编号是否存在
            while(true){
                if(temp.next == null){
                    break;
                }
                if(temp.next.no > heroNode.no){  //说明我的heroNode应该插入到这个temp的后面
                    break;
                }else if (temp.next.no == heroNode.no){
                    flag = true;
                    break;
                }
                temp = temp.next; //后移
            }
            //判断flag的值
            if (flag){
                System.out.printf("准备插入的英雄%d已经有了\n",heroNode.no);
            }else {
                //插入数据
                heroNode.next = temp.next;
                temp.next = heroNode;
            }
        }
    
    
        //显示链表
        public void list(){
            if(head.next == null){
                System.out.println("链表为空");
                return;
            }
    
            HeroNode tmp = head.next;
            while (true){
                if(tmp.next == null){
                    System.out.println(tmp);
                    break;
                }
                System.out.println(tmp);
                tmp = tmp.next;
            }
        }
    }
    
    class HeroNode{
        public int no;
        public String name;
        public String nickName;
        public HeroNode next; //指向下一个节点
        //构造器
        public HeroNode(int hNo,String hName,String hNickName) {
            this.no = hNo;
            this.name = hName;
            this.nickName = hNickName;
        }
        //重写toString
        @Override
        public String toString() {
            return "HeroNode{" +
                    "no=" + no +
                    ", name='" + name + '\'' +
                    ", nickName='" + nickName + '\'' +
                    '}';
        }
    }
    
    • 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
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
  • 相关阅读:
    Orchestrator 对 MGR MySQL Group Replication的支持
    蓄电池智能监控系统,蓄电池智能监控系统的作用
    Linux——进程:概念、创建进程(函数fork的使用、补充、总结)
    VisualStudio 制作Dynamic Link Library动态链接库文件
    leetcode 118.杨辉三角形
    【附源码】Python计算机毕业设计三亚技师学院远程作业提交系统
    搭建Java开发环境(MAC、IDEA)
    [JavaScript 刷题] 堆 - 数据流中的第 K 大元素, leetcode 703
    【数据结构】二叉树的前序遍历(七)
    【C++程序员必修第一课】C++基础课程-04:变量、常量和指针
  • 原文地址:https://blog.csdn.net/Chad_it/article/details/127805926