题解 | #从单向链表中删除指定值的节点#

从单向链表中删除指定值的节点

https://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f

import java.util.Scanner;

// mark一下 题解中用ArrayList真是绝
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) { 
            int n = in.nextInt();
            int headVal = in.nextInt();
            Node head = new Node(headVal);
            Node p = head;
            for (int i = 0; i < n - 1; i++) {
                int val = in.nextInt();
                Node x = new Node(val);
                int index = in.nextInt();
                while (p != null) {
                    if (p.val == index) {
                        x.next = p.next;
                        p.next = x;
                        break;
                    } else {
                        p = p.next;
                    }
                }
                p = head;
            }
            int target = in.nextInt();
            StringBuilder sb = new StringBuilder();
            while (p != null) {
                if (p.val != target) {
                    sb.append(p.val + " ");
                }
                p = p.next;
            }
            String ans = sb.toString();
            System.out.println(ans.substring(0, ans.length()));
            in.nextLine();
        }
    }

    static class Node {
        int val;
        Node next;
        Node(int val) {
            this.val = val;
            this.next = null;
        }
    }
}

全部评论

相关推荐

10-24 00:54
已编辑
门头沟学院 Java
牛客20646354...:这连小厂都找不到就离谱,只能说可能你根本没投什么小厂。说实话现在都要11月了,没什么岗位了。其实最好是在9月找,那时候暑假工刚走,岗位多的是,现在都占满了岗位了,秋招的秋招,顶替暑假工的也基本上都顶替了。 只能多投了,简历其实都差不多,你这都不是外卖+点评去找实习了,已经比好多人优秀了。实在找不到,可以降低一些标准的,能投到自研项目的小厂说实话可能比你去中大厂能学到更多东西。因为中大厂最多给你看一点点模块功能,小厂基本上全部代码甚至几个项目的代码都能拿到。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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