题解 | #从单向链表中删除指定值的节点#
从单向链表中删除指定值的节点
https://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
String[] chars = str.split(" ");
//char[] chars = str.toCharArray();
Node first = new Node(chars[1],null);
for (int j = 2; j < chars.length-2;j+=2) {
Node node1 = new Node(chars[j],null);
Node node2 = new Node(chars[j+1],node1);
if(first.next==null){
if(first.value.equals(node2.value)){
first.next=node2.next;
}else{
first.next=node2;
}
}else{
Node checkNode = repatCheck(first,node2);
if(checkNode.value.equals(node2.value)){
Node temp = checkNode.next;
checkNode.next = node2.next;
node2.next.next=temp;
}
}
}
String last = chars[chars.length-1];
Node firstCopy = first;
while(firstCopy!=null){
if(first.value.equals(last)){
first = first.next;
break;
}else{
if(firstCopy.next.value.equals(last)){
firstCopy.next=firstCopy.next.next;
break;
}
firstCopy = firstCopy.next;
}
}
printLink(first);
}
static void printLink(Node first) {
String c = first.value+" ";
while (first.next!=null){
first = first.next;
c+=first.value+" ";
}
System.out.println(c);
}
static Node repatCheck(Node first,Node node2) {
Node nodeCopy=first;
while(nodeCopy!=null){
if(nodeCopy.value.equals(node2.value)){
return nodeCopy;
}
if( nodeCopy.next==null){
return nodeCopy;
}
nodeCopy=nodeCopy.next;
}
return null;
}
static class Node{
String value;
Node next;
public Node(String value, Node next) {
this.value = value;
this.next = next;
}
}
}
#数据结构编程链表#
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
String[] chars = str.split(" ");
//char[] chars = str.toCharArray();
Node first = new Node(chars[1],null);
for (int j = 2; j < chars.length-2;j+=2) {
Node node1 = new Node(chars[j],null);
Node node2 = new Node(chars[j+1],node1);
if(first.next==null){
if(first.value.equals(node2.value)){
first.next=node2.next;
}else{
first.next=node2;
}
}else{
Node checkNode = repatCheck(first,node2);
if(checkNode.value.equals(node2.value)){
Node temp = checkNode.next;
checkNode.next = node2.next;
node2.next.next=temp;
}
}
}
String last = chars[chars.length-1];
Node firstCopy = first;
while(firstCopy!=null){
if(first.value.equals(last)){
first = first.next;
break;
}else{
if(firstCopy.next.value.equals(last)){
firstCopy.next=firstCopy.next.next;
break;
}
firstCopy = firstCopy.next;
}
}
printLink(first);
}
static void printLink(Node first) {
String c = first.value+" ";
while (first.next!=null){
first = first.next;
c+=first.value+" ";
}
System.out.println(c);
}
static Node repatCheck(Node first,Node node2) {
Node nodeCopy=first;
while(nodeCopy!=null){
if(nodeCopy.value.equals(node2.value)){
return nodeCopy;
}
if( nodeCopy.next==null){
return nodeCopy;
}
nodeCopy=nodeCopy.next;
}
return null;
}
static class Node{
String value;
Node next;
public Node(String value, Node next) {
this.value = value;
this.next = next;
}
}
}
#数据结构编程链表#