Linked list: delete a node in a single linked list without giving the head of the entire linked list

 

Problem Description:

There is a single-linked list containing int-type node values. Given a node in a linked list, but not given the head node of the entire linked list, delete a node in the linked list.

 

Algorithm implementation:

class Node {

public int value;
public Node next;

public Node(int value) {
this.value = value;
}

}

public void removeWiredNode(Node node) {

if(node == null) {
return;
}

Node next = node.next;
if(next == null){
throw new RuntimeException("can't remove last node");
}

node.value = next.value;
node.next = next.next;
}

 

Analysis of Algorithms:

1. Strictly speaking, it cannot be deleted because the precursor node cannot be found;

2. Explore the feasibility of deletion, and not a method for strictly restricting the problem, and give the special cases included in the solution;

3. Make a prompt that the end node cannot be deleted, and replace the node value of the non-end node.

 

Guess you like

Origin www.cnblogs.com/heibingtai/p/12682507.html