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

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

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

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String[] strs = scan.nextLine().split(" ");
        int n = Integer.valueOf(strs[0].trim());
        int head = Integer.valueOf(strs[1].trim());
        int delete = Integer.valueOf(strs[strs.length - 1].trim());
        ArrayList<Integer> arr = new ArrayList<>(6);
        arr.add(head);
        for (int i = 2; i < strs.length - 1; i += 2) {
            int l = Integer.valueOf(strs[i].trim());
            int r = Integer.valueOf(strs[i + 1].trim());
            int index = arr.indexOf(Integer.valueOf(r));
            arr.add(index + 1, l);
        }
        arr.remove(Integer.valueOf(delete));
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}
全部评论
该牛油正在参与牛客写题解薅羊毛的活动,牛币,周边,京东卡超多奖品放送,活动进入倒计时!快来捡漏啦https://www.nowcoder.com/discuss/888949?source_id=profile_create_nctrack&channel=-1
点赞
送花
回复
分享
发布于 2022-04-27 11:45

相关推荐

2 6 评论
分享
牛客网
牛客企业服务