CC150: Implements an algorithm to delete a node in the middle of a single linked list, giving only pointers to that node

Source: Internet
Author: User

Implement an algorithm to delete a node in the middle of a single linked list, giving only pointers to that node.

Example:

Input: Pointer to node C in link list a->b->c->d->e

Result: no need to return anything to get a new linked list: A->b->d->e

Answer

The key to this problem is that you only have a pointer to the node you want to delete, and if you delete it directly, the list is broken. But you can't get a pointer to the node before the node, yes, it doesn't even have a head node. In this case, you can only find another path. Re-examine this problem, we can only get from the C node after the beginning of the pointer, if you want to delete the C node after a certain node, it must be no problem. For example, to delete the node D, just point C's next pointer to E, then delete D is ok. Well, if we just delete the node D, we'll get a->b->c->e, and the target list is only one node down. What do we do? Give the D data to C! Node structure is the same, delete who is the same, the most critical is the data in the node, as long as we leave the data a->b->d->e is OK.

The idea already has, directly writes the code? Wait, before we write the code, let's briefly analyze the possible scenarios (including boundary conditions, of course). If C points to the list: 1. head node; 2. Intermediate junction. 3. Tail node. 4. Empty. The situation is normal, the data of D to C,c's next pointer to the next point of D points to the node, delete D is ok. Case 4 is empty and returns directly. Case 3 is special, if C points to the tail node, it is generally considered that the direct deletion of C is ok, anyway, c, there is no node, do not worry about the broken list. But is that really the case? The code tells us to delete C directly, and the next pointer to the C node (for example, b) is not empty. That is, if you print this list, it will print out the same list as the original linked list length, and the last element is 0!

This is what the CTCI in the original book said, which is exactly what the interviewer wants you to point out. Then, you can do some special processing, such as when C is the tail node, set its data to some special characters, so that you can print it without printing it. Of course, you can not deal with this situation directly, the original book code is to do so. Here, too, the situation is not addressed directly.

BOOL Remove (node *c) {    if (c==null | | | c->next==null) return false;    if (c->next==null) {//c is deleted as the last element, no, the last one will print a 0 node, requiring special handling    //     delete C;     return;    }    Node *q = c->next;    C->data = q->data;    C->next = q->next;    Delete q;    return true;}



CC150: Implements an algorithm to delete a node in the middle of a single linked list, giving only pointers to that node

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.