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


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

方式一:

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

方式二:

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

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());
        }

    }
}

运行结果:

在这里插入图片描述

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

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

编写方法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());
    }
}

在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 + '\'' +
                '}';
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chad__chang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值