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

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

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;

/**
 * 单链表删结点
 * 法1:创建链表
 * 法2:用linkedList
 *
 */
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s=br.readLine().split(" ");
        int[] nums= new int[s.length-3];
        int N=Integer.parseInt(s[0]);
        int delete=Integer.parseInt(s[s.length-1]);
        int head=Integer.parseInt(s[1]);
        LinkedList<Integer> list = new LinkedList<>();
        list.add(head);
        for (int i = 2; i < s.length-2; i+=2)
                 list.add(list.indexOf(Integer.parseInt(s[i+1]))+1,Integer.parseInt(s[i]));
        list.remove(list.indexOf(delete));
        list.forEach(l-> System.out.print(l+" "));
    }
}

全部评论

相关推荐

仁者伍敌:实习生要工作经验,工作要实习经验
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 14:06
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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