Algorithm learning data structure of single-linked list inversion, 22 inversion

Source: Internet
Author: User
Tags prev
One, single-linked list reversal list reversal, that is, the chain list from 1->2->3->4->5 This form to 5->4->3->2->1 this form, now can think of two ways to achieve, for what is linked list, Some of the nature of the list is not much to say, directly say two ways to achieve the following: 1, using the head node insertion method to create a new linked list. Reverse linked list, there are two ways to create a linked list (head node insertion method, tail node insertion method) know that the head knot insertion method formed by the chain list is the opposite of the input order, the tail node insertion method formed by the same list of input sequence, so one of the methods is to traverse the original linked list, Then use the original list of the output element sequence with the head node insertion method to create a new linked list, so that the new linked list is the inverted list. Specific pseudo-code operations:
Reverse (NodeList list)
if list = = NULL
    return null
NodeList newlist
Node temp = List->next
Newlist->next = NULL while temp! =
null
    temp->next = newlist->next
    newlist->next = temp
    temp = Temp->next
return newlist



2, in-place reverse linked list. Iterate through the linked list, inserting the next node of the current list's head nodes into the next node in the header, so that it can be reversed in place. such as 1->2->3->4->5 first turn into 2->1->3->4->5, and then turn into 3->2->1->4->5 so that the last node can be reversed in place. Specifically, the code operates as follows:
Reverse (NodeList list)
if list = = NULL
    return null
Node temp = List->next while
temp->next! = null< C4/>node insert = temp->next//Remove to invert node next node
    Insert->next = list->next//Insert this node behind the head node
    list->next = Insert 
    temp = temp->next//Continue traversing
return list

Two, 22 reversal 22 reversal of the single necklace table is the reversal of every two numbers. such as: C, F,->g,->d, B--------22   G-I We need to analyze four pointers, prev, current, Next, Nextnext. Analysis: We need two "pointers" pointing to the two values currently being reversed, current and next. 22 after inversion, we also need to record the next value, that is, after the reversal of A and B, we need to record the C value, we can continue to go down until the end of the list, so, we need another point to the next value of the "pointer", that is, Nextnext. After the reversal, the next of a is C, but actually, the next of a should be D, so every time we invert we need to update the next value of the previous value, that is, to change a-C to a-D, so we need to prev the pointer. So, to do this, we need 4 pointers for a total. See the code specifically.
Java code: Define the data structure first:
Class Node {  
    char value;  
    Node Next;    



Specific operation:
/** * 22 Reverses the operation, first obtains the next node and the next node of the current node, and then the next node pointer to the current node Nextnode.next = Current, the pointer of the present node only want to lower the next node Current.next = Nextnextnode, In this way, the two nodes are swapped, and then the next node value of the previous node is updated Previousnode.next = NextNode, when the update is not the first time, because the first 22 reversal does not have a previous node pointer, pointing to NULL, Then the assignment continues to loop Previousnode = CurrentNode;
	 CurrentNode = Nextnextnode; * @param list to 22 invert the link header node * @return the Inverted List header node */public static node Reverseinpair (node list) {node-current = Li  
	    St  
	    if (current = = NULL | | current.next = = NULL) {return current;
	    Node head = current.next;//holds the head node for return.  Node previousnode = null;
	        This refers to a forward node pointer while (current! = NULL && Current.next! = null) {//Get next node and next node  
	        Node nextnode = Current.next;   
	        Node Nextnextnode = Nextnode.next;  
	        Nextnode.next = current;  
	          
	        Current.next = Nextnextnode;  
	        Update the next node of the previous node if (Previousnode! = null) {Previousnode.next = NextNode;  
}	        Previousnode = current;  current = Nextnextnode;  
	The current node assignment is the next next node to continue traversal} return head;  }

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.