题解 | #从单向链表中删除指定值的节点#
从单向链表中删除指定值的节点
https://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { static List<String> linkedList = new ArrayList<>(); public static void main(final String[] args) { final Scanner sc = new Scanner(System.in); while (sc.hasNext()) { final String str = sc.nextLine(); List<String> list = new ArrayList<>(); list = new ArrayList<>(Arrays.asList(str.split(" "))); final String del = list.get(list.size() - 1); list.remove(0); Main.linkedList.add(list.get(0)); list.remove(list.size() - 1); for (int i = 1; i < list.size() - 1; i++) { Main.LinkedListAdd(list.get(i), list.get(i + 1)); } Main.LinkedListDelete(del); System.out.println(String.join(" ", Main.linkedList)); } sc.close(); } private static void LinkedListAdd(final String s, final String s1) { for (int i = 0; i < Main.linkedList.size(); i++) { if (Objects.equals(Main.linkedList.get(i), s1)) { Main.linkedList.add(i + 1, s); } } } private static void LinkedListDelete(final String s) { for (int i = 0; i < Main.linkedList.size(); i++) { if (Objects.equals(Main.linkedList.get(i), s)) { Main.linkedList.remove(i); } } } }