现在的位置: 首页 > 综合 > 正文

删除链表内地某节点, 要求时间复杂度为O(1)

2012年02月04日 ⁄ 综合 ⁄ 共 343字 ⁄ 字号 评论关闭
void delete_node(Node *head, Node *node) {

    Node *next = node->next;

    if(next==NULL) { /* if the node is the tail of the list */

      Node *curr_node = head;

      while(curr_node->next!=node) { curr_node= curr_node->next; }

      curr_node = NULL;

      delete node;

      return;

    }

    memmov(node, next, sizeof(Node));

    delete next;

  }

  其实这个办法删除的只是node的内容, 而真正删除的是node->next节点.

抱歉!评论已关闭.