Flip the list (java)

Flip two ways: the non-recursive and recursive.

1, non-recursive methods

Reverse ListNode public (ListNode Node) { 
            the Node PREV = null; 
            the while (Node = null!) { 
                // each time a new linked list for each reading cycle of operation of a node 
                ListNode Node = tmp; 
                // incoming points to a node in the linked list, the next cycle will next node reassigning tmp 
                node = node.next; 
                // set the next node is the node extracted in the last part of the reversing operation has been completed node 
                tmp.next = PREV; 
                // new node reassigning complete list of the flip part to be outputted 
                PREV = tmp; 
            } 
            // inverted output list has been completed 
            return PREV; 
  }
        

 

2, the recursive method

Reverse the Node public (ListNode Node) { 
            ListNode PREV = null; 
             // recursive determination condition, when the list is empty or only one node, recursion out 
            IF (Node == null || node.next == null) { 
                PREV Node = ; 
            } the else { 
                // completing primitive list complete from the second node to the last node among the inverted 
                ListNode tmp = reverse (node.next); 
                the first node a second node in the linked list // original point 
                node.next.next node =; 
                // first node pointing to the original empty list, the list is completed flip 
                node.next = null; 
                PREV = tmp; 
            } 
            return PREV; 
}

  

  

Guess you like

Origin www.cnblogs.com/stupid-chan/p/11351072.html