Package Cn.edu.xidian.sselab;
Import java.util.LinkedList;
/**
* Title:delete Node in a Linked List
* Content:
* Write a function to delete a node (except the tail) in a singly linked list,
* Given only access to that node.
* Supposed the linked list is 1-2, 3, 4 and you were given the third node with value 3,
* The linked list should become 1, 2, 4 after calling your function.
*/
public class Deletenodeinalinkedlist {
/**
* @author Wzy
* @param args
* @see This topic is given a single linked list, give you the node to be deleted, and then the target is to delete this node, maintain the rest of the single-linked list unchanged
* The variable is given only the node to be deleted, if it is deleted, because the single-linked list of its previous node is not known,
* So his previous node will not find the next node, this situation is not allowed.
* Here's another way of thinking, assign the value of the next node B of Node A to delete to a, and then let a point to the next node of B, so that although the next node of the specified node is deleted
* But delete does specify the value of the node, so meet the requirements
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
linkedlist<listnode> list = new linkedlist<listnode> ();
Deletenodeinalinkedlist d = new Deletenodeinalinkedlist ();
List.add (d.new ListNode (1));
List.add (D.new ListNode (2));
List.add (D.new ListNode (3));
List.add (D.new ListNode (4));
List.add (D.new ListNode (5));
for (int i=0;i<4;i++) {
List.get (i). Next = List.get (i + 1);
}
List.get (4). next = null;
D.deletenode (List.get (3));
List.remove (3);
System.out.println (List.size ());
for (int i=0;i<list.size (); i++)
System.out.println (List.get (i). val);
}
public class listnode{
int Val;
ListNode Next;
ListNode (int x) {
val = x;
}
}
public void Deletenode (ListNode node) {
if (node! = NULL | | Node.next! = NULL) {
Node.val = Node.next.val;
Node.next = Node.next.next;
}
}
}
Delete Node in a Linked List