1 /**2 * Delete Linked list nodes at O (1) Time3 * 4 * @author5 *6 */7 Public classSolution {8 9 Public Static voidDeletenode (node Head, node Deletednode) {Ten if(NULL= = Head | |NULL==Deletednode) { One return; A } - - if(Deletednode.next! =NULL) {//Delete is not a tail node the -System.out.println ("1-----"); - -Node NextNode =Deletednode.next; +Deletednode.value =Nextnode.value; -Deletednode.next =Nextnode.next; + A}Else if(head = = Deletednode) {//the tail node is deleted, and the tail node is the head node, and the entire list has only one node at -System.out.println ("2-----"); - -Head =NULL; -Deletednode =NULL; - in}Else{//the tail node is deleted, but the list is more than one node - toSystem.out.println ("3-----"); + - //iterate through the list, find the previous node of the tail node, and set next to null theNode Pnode =head; * while(Pnode.next! =Deletednode) { $Pnode =Pnode.next;Panax Notoginseng } - thePnode.next =NULL; +Deletednode =NULL; A } the } + - Public Static voidMain (string[] argss) { $ $Node third =NewNode (2,NULL); -Node second =NewNode (1, third); -Node first =NewNode (0, second); the - Deletenode (first, third);Wuyi the } - } Wu - classNode { About $ intvalue; - Node Next; - - PublicNode (intvalue, Node next) { A This. Value =value; + This. Next =Next; the } - $}
"Linked list" at O (1) time to delete a linked list of nodes