Interview question 16: reverse linked list

Source: Internet
Author: User
Question: define a function, enter the head node of a linked list, reverse the linked list, and output the head node of the reverse linked list.

Suppose there is a linked list A-> B-> C-> D-> E-> F-> G. In a phase of the chain table reversal process, the linked list Pointer Points to a <-B <-C <-d e-> F-> G. That is to say, all the nodes before node D have been reversed, and all the nodes starting with node E after node D have not been reversed. In this way, there is a fracture between D and E. If we want to reverse the linked list, we will take the following important steps:

    1. D-> E changes to D-> C, and the pointer is reversed.
    2. Move the pointer one to the next node E.
    3. Combined with 1.2, we found that three pointers must be operated, namely C, D, and E.

Therefore, you can consider storing the pointers of the three nodes C/D/E, and use the pointers of these three nodes to reverse.

CodeInstance:

View code

# Include <iostream> # Include <Stdlib. h> # Include <Stack> Using   Namespace  STD; //  Linked List Structure  Struct  Listnode {  Int  M_nvalue; listnode * M_pnext ;};  //  Create a linked list Node Listnode * createlistnode ( Int  Value) {listnode * Pnode = New  Listnode (); pnode -> M_nvalue = Value; pnode -> M_pnext =NULL;  Return  Pnode ;}  //  All nodes in the traversal chain table  Void Printlist (listnode * Phead) {listnode * Pnode = Phead;  While (Pnode! = Null) {cout <Pnode-> m_nvalue < "   "  ; Pnode = Pnode->M_pnext;} cout < Endl ;}  //  Add a node to the end of the linked list  /*  Note that phead is a pointer to a pointer, which is typically referenced in the main function. If you want to add nodes to the linked list, the structure of the linked list will be modified. Therefore, you must pass a reference to save the modified structure.  */  Void Addtotail (listnode ** phead, Int  Value) {listnode * Pnew = New Listnode (); //  Newly inserted Node Pnew-> m_nvalue =Value; pnew -> M_pnext = NULL;  If (* Phead = NULL) //  Empty linked list  { * Phead = Pnew ;}  Else  {Listnode * Pnode = * Phead;  While (Pnode-> m_pnext! = Null) pnode = Pnode-> M_pnext; pnode -> M_pnext =Pnew ;}} listnode * Reverselist (listnode * Phead) {listnode * Pnode = phead; //  Current Node Listnode * pprev = NULL; //  The previous node of the current node Listnode * preversedhead = NULL; //  Invert the linked list header Node      While (Pnode! = Null) {listnode * Pnext = pnode-> M_pnext;  If (Pnext = NULL)//  If the next node of the current node is empty, the head node of the reverse linked list is the current node. Preversedhead = Pnode; pnode -> M_pnext = pprev; //  The current node refers to the forward node.  Pprev = Pnode; //  Pprev and pnode move forward. Pnode = pnext; //  Here we need to use the previously saved pnext, instead of pnode-> m_pnext  }  Return Preversedhead; // Returns the inverted linked list header pointer.  }  Void  Main (){  //  Create a node Listnode * pnode1 = createlistnode ( 1 ); //  Create a node Printlist (pnode1 ); //  Print  //  Add a new node to the linked list Addtotail (& pnode1, 2 ); // Add a node to the linked list Addtotail (& pnode1, 3 ); //  Add a node to the linked list Addtotail (& pnode1, 4 ); //  Add a node to the linked list Addtotail (& pnode1, 5 ); //  Add a node to the linked list Addtotail (& pnode1, 6 ); //  Add a node to the linked list Addtotail (& pnode1,7 ); //  Add a node to the linked list  //  Print linked list Printlist (pnode1 ); //  Print  //  Reverse linked list Listnode * preversedhead = Reverselist (pnode1); printlist (preversedhead );  //  Print  System (  "  Pause "  );} 

Running result:

1
1 2 3 4 5 6 7
7 6 5 4 3 2 1

PS: 2012-5-3

It is found that there is a more concise way, that is, the preversedhead pointer is not required.ProgramWe can see that the preversedhead pointer is simply used to save the head pointer of the reverse linked list, but the pprev and pnode pointers actually contain the head pointer of the reverse linked list, therefore, we do not need to define a separate header pointer for saving. The modified function is as follows:

View code

Listnode * reverselist2 (listnode * Phead) {listnode * Pnode = phead; //  Current Node Listnode * pprev = NULL; //  The previous node of the current node      While (Pnode! =Null) {listnode * Pnext = pnode-> M_pnext; pnode -> M_pnext = pprev; //  The current node refers to the forward node.  Pprev = Pnode; //  Pprev and pnode move forward. Pnode = pnext; //  Here we need to use the previously saved pnext, instead of pnode-> m_pnext  }  Return Pprev; //  Returns the inverted linked list header pointer. }

 

 

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.