Delete a linked list node at O (1) Time--13

Source: Internet
Author: User

Given the head pointer of a unidirectional list and a node pointer, define a function to delete the node at O (1) time.


Because the requirement time complexity is O (1), so certainly can not traverse the chain list, so the time complexity is O (N), can think of, actually asked to delete the node, the real goal is not to the node data including the memory of the node is deleted, just want to let the data disappear on it, as to the node, The space occupied by any one node is OK;

So, here's another way of thinking, to delete the specified node, it is generally necessary to point the previous node's next pointer to the next node to delete the node, so that the deletion of the node can be removed from the list, but the key here is that the single-linked list does not have a prev pointer and cannot traverse the list to find the previous node. But you can know to delete the next node and the next node, ah, so you can replace the deletion method, with the next node to replace the node to be deleted to "die", and the two data only need to exchange a bit, so the deletion of the node location is the next node, and the data is to delete the data;

The program is as follows:

#include  <iostream> #include  <assert.h>using namespace std;template < class t>struct listnode  //linked list node structure body {    t _data;     ListNode<T>* _next;}; Template <class t>listnode<t>* buy_node (t data)   //build a linked table node {     ListNode<T>* tmp = new ListNode<T>;     tmp->_data = data;    tmp->_next = null;     Return tmp;} Template <class t>void init_list (Listnode<t>** node, t data)     //Initialize linked list {    *node = buy_node (data);} Template <class t>void push_node (Listnode<t>*& head, t data)    //link Node {    if (head == null)     {&nb in linked listSp;          init_list (&head, data);         return;    }        Listnode<t>* tmp = head;    while (Tmp->_next != null)     {           tmp =  Tmp->_next;    }    tmp->_next = buy_node (data);} Template <class t>void print_list (listnode<t>* head)   //Print List {     while (head != null)     {         cout<


Run the program and get the result:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7F/E2/wKiom1cwktygiHbmAAAH3rvbTyk911.png "title=" Deletenode.png "alt=" Wkiom1cwktygihbmaaah3rvbtyk911.png "/>



Finish

This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1771598

Delete a linked list node at O (1) Time--13

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.