List in reverse order --- python

class ListNode: 
    the Value = ''   # node value to be stored, because Python is weakly typed, it is not necessary incoming generic 
    the Next = None   # next node, initialized to a null value 

    DEF  the __init__ (Self, value):   # initialization, default initialization node, the node value to be stored (value) must be given. 
        = self.Value value 
        self.Next = None 

    DEF set_next (Self, Next):   # specified node (Next). 
        = self.Next Next 

    DEF the get_value (Self):   # Gets the value of the current node stored 
        return self.Value 

    DEF get_next (Self):   # Get the next node 
        returnself.Next 


class List: 
    Head = None   # head node 

    DEF  the __init__ (Self):   # initialization function list 
        self.Head = ListNode (0)   # initialization list initialization i.e. the first node, a value of 0 within the 

    DEF the Add ( Self, value):   # Add function node 
        node = self.Head 
        node.Value + =. 1   # header length stored in the node + 1'd 
        the while node.Next iS  Not None: 
            node = node.Next 
        node.Next = ListNode (value )   # initialize the new node, and added to the end 

    defprint_list (Self):   # Printing List function 
        Node = self.Head
         Print ( ' of The List the Length IS ' , End = '' )
         Print (node.get_value (), End = ' : [ ' )   # print a listing about the total length 
        if node.Next iS None:   # If the list is empty ends directly 
            Print ( " ] " )
             return 
        node = node.Next   # scratch the next node node starts to operate 
        the while node.Next iS  Not None:
            Print (node.get_value (), End = ' , ' )   # print node value 
            Node = node.Next   # next node 
        Print (node.get_value (), End = ' ] \ n- ' )   # print the last node and a line feed 

    DEF Reverse (Self):   # the reverse function list 
        Print ( " ! reverse operation " ) 
        head = self.Head   # head out acquire node 
        IF head.Next IS None:
             return   # if only the header node, will be directly returned 
        node head.Next =   #Scratch the next node starts to operate a node 
        temp = None   # provisional value of the recording of the next node in 
        pre = None   # nonce recording a node of 
        the while node.Next IS  Not None:   # current node is not a null cycle when 
            temp = node.Next   # to keep in mind the next node of this node 
            node.Next pre =   # let this node point to a core nodes [operation] 
            pre = the node   # after the temporary pointer on a node shift 
            the node = the TEMP   # after this node shift 
        node.Next pre =   # when the end of the cycle, the last node before the node point, to complete the list reverse 
        head.Next = node   # will point to the new head of the linked list node start node 


iF  the __name__ ==' __Main__ ' :   # main function begins 
    my_list = List ()   # initialization list 
    for I in Range (0, 10 ): 
        my_list.add (I)   # increasing node values 
    my_list.print_list ()   # print at 
    my_list.reverse ()   # reverse operation 
    my_list.print_list ()   # and then print it and see

 

Guess you like

Origin www.cnblogs.com/turningli/p/12483836.html