题解 | #【模板】链表#

【模板】链表

https://www.nowcoder.com/practice/97dc1ac2311046618fd19960041e3c6f

import java.util.Scanner;

/**
 * @BelongsProject: homework
 * @Author: Ling xi_Li
 * @CreateTime: 2023-10-25 12-40
 * @Description: TODO
 */

class Node {
    private int data;
    private Node next;

    public Node() {
        this.data = 0;
        this.next = null;
    }

    public Node(int data) {
        this.data = data;
        this.next = null;
    }

    public Node getNext() {
        return next;
    }

    public int getData() {
        return data;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}

class Link {

    //TODO head是带空节点的头指针
    private Node head;

    public Link() {
        head = null;
    }

    public Link(Node head) {
        this.head = head;
    }

    public boolean isEmptyLink() {
        return head.getNext() == null;
    }

    public void append(Node node) {
        if (isEmptyLink()) {
            this.head.setNext(node);
        } else {
            Node temp = this.head.getNext();
            while (temp.getNext() != null) {
                temp = temp.getNext();
            }
            temp.setNext(node);
        }
    }

    public void add(Node node) {
        if (isEmptyLink()) {
            this.head.setNext(node);
        } else {
            node.setNext(this.head.getNext());
            this.head.setNext(node);
        }
    }

    public void insert(int x, int y) {
        Node node = new Node(y);

        int flag = 0;
        Node temp = this.head;
        while (temp.getNext() != null) {
            if (temp.getNext().getData() == x) {
                node.setNext(temp.getNext());
                temp.setNext(node);
                flag = 1;
                break;
            }
            temp = temp.getNext();
        }
        if (flag == 0) {
            temp.setNext(node);
        }
    }

    public void remove(int num) {

        if (isEmptyLink()) return;

        Node temp = this.head;

        Node current = temp.getNext();

        while (current != null) {
            if (current.getData() == num) {
                temp.setNext(current.getNext());
                current.setNext(null);
                break;
            }
            temp = current;
            current = current.getNext();
        }
    }

    public void show() {

        if (isEmptyLink()) {
            System.out.println("NULL");
            return;
        }
        Node temp = this.head.getNext();
        while (temp != null) {
            System.out.print(temp.getData() + " ");
            temp = temp.getNext();
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别

        Link list = new Link(new Node());
        int n = in.nextInt();
        for (int i = 0; i < n; i++) {
            String op = in.next();
            if (op.equals("insert")) {
                list.insert(in.nextInt(), in.nextInt());
            } else if (op.equals("delete")) {
                list.remove(in.nextInt());
            }
        }
        list.show();
    }
}

全部评论

相关推荐

一共一个小时,面试难度以及自己的回答算是最近的面试压力比较大的,实习问了30分钟,中间穿插八股。1.redis数据结构2.redis持久化机制3.mysql索引底层4.聚簇索引与非聚簇索引5.索引优化6.索引失效7.mysql执行一条sql8.那么多索引mysql怎么选(不会)9.tcp与udp区别10.tcp为什么可靠11.消息队列作用12.kafka怎么保证消息有序性13.mcp是什么?14.skills是什么?15.jvm内存分配与回收过程(我讲了从创建对象到判断垃圾对象到垃圾回收我全说了一遍,是这个吗?)16.fullgc触发机制17.tcp的拥塞控制流程(不会了)18.分布式事务解决方案,说了2pc,3pc,tcc。算法是反转双向链表,没有按格式输出,但是面试官没让继续写了,面完以为挂了,结果晚上秒过,看看复试什么情况吧。今天百度打电话准备发offer了,业务跟在手子的差不多,很垂,并且说不分日常暑期,只看表现,会有转正机会,但是考虑再三还是拒绝了,百度实习薪资确实有点低,title也不如之前了,但是面试的二位业务老师我很喜欢,对我的评价也不错,希望之后能有机会共事。从三月份到现在一共面了六家,面试次数总共是8场,情况如下:脉脉二面(无答复,默认挂)百度二面已oc美团一面过,下周一二面shein一面过直接HR面游族一面过直接HR面腾讯一面过等待约二面滴滴明天一面面试通过率还是蛮高的,但是大部分都是日常,感觉对我现在的加成不大,大概率不会去,不知道暑期会是什么情况呢唉,希望能有面试吧,继续加油。字节被无hc直接取消了,现在还没人捞,有没有字节HR救救我
不管什么都不想跳动了:本人美团百度快手都待过,建议肯定是直接留快手多一点产出后转正or直接冲字节腾讯暑期吧。一是快手从福利到基建都吊打另外两家。美团现在这个业务比较惨,本来毛利就很低,亏损严重,今年很可能要优化人力降低成本,去了别说日常,就算暑期后面都很可能被优化。百度其实实习生权限挺高的,可以接触到一些含金量高的项目,但是现在的风评不如之前了,薪资也不高。二是转正概率和薪资是跟产出挂钩的,你都在手子已经积累产出了,去其他家日常实习产出都是从0开始,肯定不可能有你在手子转正可能性大啊,现在日常压根没必要去,而且我有两个师弟都是在快手日常转正的,不用太担心,安心留在手子一边多做一点产出然后一边冲字节腾讯暑期,字节腾讯今年实习岗位非常多的,不如好好把握这个,加油。
今天你投了哪些公司?
点赞 评论 收藏
分享
03-12 15:35
嘉应学院 Python
M_地球online...:真“boss直聘”
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务