Delete the access to a single node in the programmer interview template.

Source: Internet
Author: User

Delete the access to a single node in the programmer interview template.

[Disclaimer: All Rights Reserved. indicate the source for reprinting. Do not use it for commercial purposes. Contact mailbox: libin493073668@sina.com]


Question link: http://www.nowcoder.com/practice/75e0235da04141d4823eb74018e9e0bc? Rp = 1 & ru =/ta/cracking-the-coding-interview & qru =/ta/cracking-the-coding-interview/question-ranking


Description
Implement an algorithm to delete a node in the middle of a one-way linked list. Assume that you can only access this node.
If a node with deletion is specified, perform the delete operation. If the node is the end node, false is returned; otherwise, true is returned.

Ideas
How can we delete a node that cannot be found at the previous node? We can change our thinking. First, we can assign the next node to this node, which is equivalent to deleting this node, but at this time there are duplicate nodes in the linked list, we just need to point the next of the node to the next of the original node to complete the deletion operation.


/*struct ListNode {    int val;    struct ListNode *next;    ListNode(int x) : val(x), next(NULL) {}};*/class Remove{public:bool removeNode(ListNode* pNode){// write code hereif(pNode==nullptr || pNode->next==nullptr)return false;ListNode *pNext = pNode->next;pNode->val = pNext->val;pNode->next = pNext->next;return true;}};


Copyright Disclaimer: This article is the original article of the blogger. If it is reproduced, please indicate the source

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.