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

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

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


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = Integer.parseInt(sc.next());
            LinkedList<Integer> list = new LinkedList<>();
            int head = Integer.parseInt(sc.next());
            list.add(head);
            for (int i = 1; i < n; i++) {
                int pre = Integer.parseInt(sc.next());
                int cur = Integer.parseInt(sc.next());
                int indexInsert = list.indexOf(cur) + 1;
                list.add(indexInsert, pre);
            }
            int delete = Integer.parseInt(sc.next());
            list.removeIf(integer -> integer.equals(delete));
            if (list.size() == 0) {
                System.out.println("null");
            } else {
                for (Integer integer : list) {
                    System.out.print(integer + " ");
                }
            }
        }
    }
}
全部评论

相关推荐

09-29 11:19
门头沟学院 Java
点赞 评论 收藏
分享
10-15 09:13
已编辑
天津大学 soc前端设计
点赞 评论 收藏
分享
点赞 1 评论
分享
牛客网
牛客企业服务