18 to delete the list of nodes

Title: delete list node in O (1) time to the order to the head pointer list and a pointer to a node.

delete_list_node DEF (the root, RP): 
    # junction point where only one 
    IF the root == RP: 
        the root = root.next 
        del RP 
        return the root 

    # deleted is the last node where 
    IF rp.next == None: 
        Q = the root 
        ! = q.next the while RP: 
            Q = q.next 
        q.next = None 
        del RP 
        return the root 
    the else: 
        Q = rp.next 
        rp.data = q.data 
        rp.next = q.next 
        del Q 
        return the root


Case 1: Remove the head node is directly returned head.next. (This case the list contains only a head node)

Case 2: delete the last node of the list, you need to traverse the list to get the precursor node to remove the node, then the node pointer to None, delete nodes

Case 3: Remove the intermediate node, replicate the successor node value of a node to the current node, then let pointer to a subsequent successor

Guess you like

Origin www.cnblogs.com/kingshine007/p/11350497.html