I. Title: Delete Linked list nodes at O (1) time
title: give an order to the list of head pointers and a node pointer, define a function at O (1) Time to delete the node.
The original text is in C + +, where C # is used, and the nodes are defined as follows:
public class node<t> {// data field publ IC T Item {get; set// pointer field public node<t > Next {get; setpublic Node () {} public Node (T item) {this. Item = item;}
The Deletenode method to be implemented is defined as follows:
void Deletenode (node<int> headnode, node<int> deletenode) {}
Second, the problem-solving ideas 2.1 conventional ideas
To delete a node in a one-way list, the most common practice is to start with the head node of the linked list, iterate through the nodes to be deleted, and delete the node in the linked list. The time complexity is naturally O (n)because of the need for sequential lookups.
2.2 Correct thinking
Is it necessary to get the previous node of the deleted node? The answer is in the negative.
We can easily get the node of the node to be deleted. Therefore, we can copy the contents of the next node to the node that needs to be deleted, overwrite the original content, and then delete the next node, which is the equivalent of deleting the node that needs to be deleted at this point.
However, there are two special cases that need to be considered:
(1) If the node to be deleted is at the end of the list, then it has no next node:
At this point we are still starting from the head node of the list, sequentially traversing to get the node's pre-order node, and complete the delete operation, which still belongs to the O (n) time operation.
(2) If there is only one node in the list, and we delete the list's head node (also the tail node):
At this point, after we delete the node, we also need to set the head node of the list to null.
Finally, by synthesizing the worst case scenario (the tail node needs sequential lookups, 1 times) and the best case (n-1 times), the average time complexity is:
It is important to note that by the O (1) time limit, we have to push the responsibility of ensuring that the node is in the linked list to the caller of the function Deletenode .
Third, solve the problem 3.1 code implementation
PublicStaticvoid Deletenode (node<Int> Headnode, node<Int>Deletenode) {if (Headnode = =null | | Deletenode = =Null) {Return; }if (deletenode.next! =Null//Linked list has multiple nodes, not tail node to delete: O (1) Time{node<Int> Tempnode =Deletenode.next; Deletenode.item =Tempnode.item; Deletenode.next =Tempnode.next; Tempnode =Null; }Elseif (Headnode = = Deletenode)// list only one node, delete head node (also tail node): O (1) Time {Deletenode = nullnullelse // {Node<int> Tempnode = Headnode; while (Tempnode.next!= Deletenode) {Tempnode = Tempnode.next; } Tempnode.next = nullnull
3.2 Unit Test
(1) Package return results
This method is used as a comparison of unit tests to compare the actual values with the expected value:
public static String Getprintnodes (Node<int> Headnode) {
if (Headnode = =
nullstringnew StringBuilder (); while (Headnode! = null Headnode.next;} return sbnodes.tostring ();}
(2) Test Cases
//There are multiple nodes in the list, delete the middle nodes.[TestMethod]PublicvoidDeleteNodeTest1 () {node<int> Head1 =New node<Int> (1); node<Int> head2 =New node<Int> (2); node<Int> head3 =New node<Int> (3); node<Int> head4 =New node<Int> (4); node<Int> HEAD5 =New node<Int> (5); Head1. Next =head2; Head2. Next =Head3; Head3. Next =HEAD4; Head4. Next =HEAD5; Program.deletenode (Head1, head3); Assert.AreEqual (Program.getprintnodes (HEAD1),"1245"); }//There are multiple nodes in the linked list, delete the tail nodes.[TestMethod]PublicvoidDeleteNodeTest2 () {node<int> Head1 =New node<Int> (1); node<Int> head2 =New node<Int> (2); node<Int> head3 =New node<Int> (3); node<Int> head4 =New node<Int> (4); node<Int> HEAD5 =New node<Int> (5); Head1. Next =head2; Head2. Next =Head3; Head3. Next =HEAD4; Head4. Next =HEAD5; Program.deletenode (Head1, HEAD5); Assert.AreEqual (Program.getprintnodes (HEAD1),"1234"); }//There are multiple nodes in the list, deleting the head node.[TestMethod]PublicvoidDeleteNodeTest3 () {node<int> Head1 =New node<Int> (1); node<Int> head2 =New node<Int> (2); node<Int> head3 =New node<Int> (3); node<Int> head4 =New node<Int> (4); node<Int> HEAD5 =New node<Int> (5); Head1. Next =head2; Head2. Next =Head3; Head3. Next =HEAD4; Head4. Next =HEAD5; Program.deletenode (Head1, head1); Assert.AreEqual (Program.getprintnodes (HEAD1),"2345"); }//There is only one node in the list, delete the head node.[TestMethod]PublicvoidDeleteNodeTest4 () {node<int> Head1 =New node<int> (1null// [TestMethod] Span style= "color: #0000ff;" >public void DeleteNodeTest5 () {Program.deletenode ( Span style= "color: #0000ff;" >null, null); Assert.AreEqual (Program.getprintnodes (" " Span style= "color: #000000;" >); }
Test Pass Result:
(3) Code coverage
Zhou Xurong
Source: http://edisonchou.cnblogs.com
The copyright of this article is owned by the author and the blog Park, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to give the original link.
Delete a linked list node at O (1) time