Reverse a linked list

Source: Internet
Author: User

There are many ways to reverse the linked list. One is to move the pointer to the last element every time, and take this element down, put it in the header of A New linked list, and end with the reverse, scanning the chain table n-1 times in total. Obviously, this is relatively slow. Another method is to change the pointer direction from the Start Node to the end, and scan the linked list from the start to the end, which is much more efficient than the previous one. The code for this method is as follows:

# Include
 
  
# Include
  
   
Typedef struct Info {int data; struct Info * next;} Node, * pNode; int main () {void create (pNode); void reverse (pNode ); void print (pNode); pNode link; link = (pNode) malloc (sizeof (Node); create (link); reverse (link); print (link ); return 0;} void create (pNode head) {int n = 0; pNode p, q; p = q = (pNode) malloc (sizeof (Node )); scanf ("% d", & p-> data); while (p-> data! = 0) {++ n; if (n = 1) head-> next = p; else q-> next = p; q = p; p = (pNode) malloc (sizeof (Node); scanf ("% d", & p-> data);} q-> next = NULL;} void reverse (pNode head) {pNode p0, p1, p2; if (head-> next = NULL & head-> next = NULL) /* If this linked list does not have one or only one element, you do not need to perform reverse operations */return; p0 = NULL; p2 = head-> next; /* p2 points to the first element */p1 = head-> next;/* p1 points to the second element */while (p1! = NULL) {p2-> next = p0;/* First, disconnects the link between the elements that p1 points to and the elements that p2 points, let p2 point to the element's pointer to its previous element */p0 = p2;/* the following three steps have completed the pointer backward shift and prepare the next cycle */p2 = p1; p1 = p1-> next;} p2-> next = p0;/* The Last Time p1 is empty, the loop is exited, so we need to execute another operation to let p2 point to the element's pointer to its previous element */head-> next = p2; /* change the header node to */} void print (pNode head) {pNode p = head-> next; while (p! = NULL) {printf ("% d", p-> data); p = p-> next;} putchar (10 );}
  
 


Related Article

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.