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

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

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int nodeSum=sc.nextInt();
        ListNode head=new ListNode(sc.nextInt(),null);

        int count=0;
        while(count<nodeSum-1){ //hasNext()无法返回false
            int first=sc.nextInt();
            int second=sc.nextInt();
            Insert(head,first,second);
            count++;

        }
        int k=sc.nextInt();


        ListNode vhead=new ListNode(0,head);
        ListNode index=vhead;
        while(index!=null&&index.next!=null){
            if(index.next.key==k){
                index.next=index.next.next;
            }
            index=index.next;
        }

        while(head!=null){
            System.out.print(head.key+" ");
            head=head.next;
        }

    }

    public static void Insert(ListNode head,int first,int second){
        ListNode index=head;
        while(index!=null){
            if(index.key==second){
                ListNode temp=index.next;
                index.next=new ListNode(first,temp);
                return;
            }
            index=index.next;
        }

    }
}


class ListNode{
    int key;
    ListNode next;

    public ListNode(int key,ListNode next){
        this.key=key;
        this.next=next;
    }
}

全部评论

相关推荐

这算盘打的
程序员小白条:都这样的,都是潜规则,你自己说可以实习一年就行了,实习可以随便跑路的
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-07 13:35
虽然不怎么光彩,经过这件事,可能我真的要去认同“面试八股文早该淘汰!不会用AI作弊的程序员=新时代文盲!”这句话了
HellowordX:Ai的出现是解放劳动力的,不是用来破坏公平竞争环境的,这样下去,轻则取消所有线上面试,严重了会影响整个行业对所有人产生影响,企业会拉高入职考核各种离谱考核会层出不穷
你找工作的时候用AI吗?
点赞 评论 收藏
分享
frutiger:逆天,我家就安阳的,这hr咋能说3k的,你送外卖不比这工资高得多?还说大厂来的6k,打发叫花子的呢?这hr是怎么做到说昧良心的话的
找工作时遇到的神仙HR
点赞 评论 收藏
分享
见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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