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.

Examples:

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 point is that you just 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 such a case, you can only find another path. Another look at this question, we can only get from the C node after the beginning of the pointer, assuming that you delete a node after the C node, it must be no problem. For example, to delete a node d, simply point C's next pointer to E, and delete D will be OK. OK, let's say we delete node D, we'll get a->b->c->e, and the target list is just one node down. What do we do? Give the D data to C! Node structure is the same, delete who are the same, the most critical is the data in the node, just want us to leave the data a->b->d->e is OK.

The idea already has, directly writes the code? And so on, before we write the code, let's briefly analyze the various situations that may occur (including the boundary condition, of course). Suppose 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 more special, assuming that C points to the tail node, usually feel that the direct deletion of C is ok, anyway, c, there is no node, do not worry about the list disconnected. But is that really the case? The code tells us that the next pointer to the C node (say B) is not empty, just delete C. That is, if you print the list, it will print out the same list as the length of the original list, 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, for example, when C is a 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 code in the book is to do so. Here, too, the situation is not dealt with 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.