[Algorithm analysis] how to delete the node of a single-chain table in O (1) Time

Source: Internet
Author: User

The question is as follows: give you a single-chain table header, and give you a pointer to a node. to delete this node, the condition is that your program must be in O (1).

Some people are not very familiar with the linked list. This article tries its best to make it easy to understand. Please skip the previous section.

The linked list structure is as follows:

 
Struct node {int val; node * Next ;};

The question is not very difficult, and you will soon be able to think of a good way :)

First, let's review the common deletion method. First, use the header to locate the previous node (set to a) of the node to be deleted (set to B), and change the pointing of, delete node B.Delete the node to be deleted. This is not only a good habit, but also can avoid serious problems that may cause memory leakage in future projects.

Void deletenode_on (node * linklist, node * P) {printf ("delete: % d \ n", p-> Val); node * s = linklist; while (S-> next! = P) S = s-> next; s-> next = s-> next; Delete (p );}

This algorithm mainly takes time to find the previous node, so it is an O (n) algorithm.

Therefore, since the requirement is O (1), it is obvious that the for operation cannot be performed again. Considering the deletion of arrays, this problem is better solved.

First, we can easily get the node to be deleted, that is, the next node C of Node B, and then assign the value of C to the value of Node B, which is equivalent to overwriting when the array is deleted, now Node B and node C are exactly the same. Next we can simply delete node B without deleting Node B. Simply delete node C by using node B. The method is simple and the time is O (1 ).


But think about the obvious defect of this algorithm. What if the node to be deleted is the last node? At this time, it seems that there is no good solution, and it can only be honest O (n. Now let's look at the average time complexity:

Meets the question requirements.

 
Void deletenode_o1 (node * linklist, node * P) {printf ("delete: % d \ n", p-> Val); If (p-> next! = NULL) // If P is not the end node, overwrite P from the last node, and delete the last node {P-> val = p-> next-> val; node * TMP = p-> next; P-> next = p-> next; Delete (TMP);} else // If P is the end node, find the previous node of P and delete {node * s = linklist; while (S-> next! = P) S = s-> next; s-> next = s-> next; Delete (p );}}

Complete Test code is attached:

# Include <iostream> using namespace STD; struct node {int val; node * Next;}; void createlinklist (node * linklist) {node * s = linklist; for (INT I = 0; I <10; I ++) {node * t = new node; t-> val = I; t-> next = NULL; S = s-> next = T;} void showlinklist (node * linklist) {node * s = linklist-> next; while (S = s-> next) printf ("% d", S-> Val); putchar (10);} void deletenode_on (node * linklist, node * P) {printf ("DELE Te: % d \ n ", p-> Val); node * s = linklist; while (S-> next! = P) S = s-> next; s-> next = s-> next; Delete (p);} void deletenode_o1 (node * linklist, node * P) {printf ("delete: % d \ n", p-> Val); If (p-> next! = NULL) // If P is not the end node, overwrite P from the last node, and delete the last node {P-> val = p-> next-> val; node * TMP = p-> next; P-> next = p-> next; Delete (TMP);} else // If P is the end node, find the previous node of P and delete {node * s = linklist; while (S-> next! = P) S = s-> next; s-> next = s-> next; Delete (p) ;}} int main () {node * linklist = new node; createlinklist (linklist); showlinklist (linklist); node * P = linklist-> next; For (INT I = 0; I <3; I ++) P = p-> next; deletenode_on (linklist, P); showlinklist (linklist); P = linklist-> next; For (INT I = 0; I <8; I ++) P = p-> next; deletenode_o1 (linklist, P); showlinklist (linklist); P = linklist-> next; for (INT I = 0; I <4; I ++) P = p-> next; deletenode_o1 (linklist, P); showlinklist (linklist); getchar (); return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.