Reverse the order of a linked list C + +

Source: Internet
Author: User

First we define a head node:

    struct node{          int data;          Node*next;      };  

Next we write a function to create a linked list:

    A is an array, n represents the number of array elements      node*createlinklist (int a[],int n)      {          if (a==null| | n==0)              return NULL;          Node*l,*t=null;          for (int i=0;i<n;i++)          {              if (i==0)              {l=new Node;              l->data=a[i];              l->next=null;              t=l;              } else              {                  node*s=new Node;                  s->data=a[i];                  s->next=null;                  t->next=s;                  t=s;//node Move              }          }          return L;      }  

Let's start with the Flip method section below.

1) First, we'll start with a recursive approach.

(1) If a linked list is an empty list, then his reverse order is still empty.

(2) If there is only one node in a linked list, then his reverse order is the list itself.

(3) If the length of a linked list is greater than one, then we do the following recursion.

Reverse the list of the remaining nodes in the current list except the Head node L, which is called recursively, and get the head node p after the reverse of the remaining list,

The next node of L is then directed to L and the next pointer of L is empty. Then return p.

(Simply put, first a layer of search, find the tail node to return to P, and then return to the previous layer, then the last node is L->next, the L->next next point to the second level of L, and then the L next empty, that is completed the first flip, and then return to P , think of the two nodes as the last one, and then repeat the reverse at the bottom of the third level, and finally complete the reversal.

    Node*reverselinklist (node*l)      {              if (l==null) return NULL;          if (l->next==null) return L;                        Node*p=reverselinklist (l->next);              l->next->next=l;//the first point to the penultimate              l->next=null;//the penultimate pointer is empty                    return p;        

2) The second method is not recursive to flip, but we directly at the end of the flip will appear broken chain, and thus can not be continuous traversal, so we could define three nodes, Pnow point to the current node, the pre point to its predecessor, the NEX point to its rear drive, the first point of Pnow's next pointer to the NEX, Then the Pnow next is removed and connected to the pre, flip, then Pnow and the NEX is disconnected, and then set the Pnow to the precursor pre, the NEX is set to Pnow, that is, the completion of a rollover, repeat to complete.

    Node*noreverselinklist (node*l)      {          if (l==null| | L->next==null) return L;          Node*pnow=l,*pre=null,*nex=null,*tail=null;          while (Pnow!=null)          {              nex=pnow->next;              if (Null==nex) {                  tail=pnow;}              pnow->next=pre;              Pre=pnow;              Pnow=nex;                        }                    return tail;      }  

To verify, we also write a display function:

    void display (Node*l)      {  node*p=l;          if (p==null) return;          while (P!=null)          {              cout<<p->data<< "";              p=p->next;          }          cout<<endl;            }  

Full code:

Include<iostream> using namespace std;      struct node{int data;  Node*next;  }; A is an array, n represents the number of array elements node*createlinklist (int a[],int n) {if (a==null| |      n==0) return NULL;      Node*l,*t=null;          for (int i=0;i<n;i++) {if (i==0) {l=new Node;          l->data=a[i];          l->next=null;          T=l;              }else {node*s=new Node;              s->data=a[i];              s->next=null;              t->next=s;  t=s;//node Move}} return L;      } void Display (node*l) {node*p=l;      if (p==null) return;          while (p!=null) {cout<<p->data<< "";      p=p->next;    } cout<<endl;      } node*reverselinklist (Node*l) {if (l==null) return NULL;                if (l->next==null) return L;          Node*p=reverselinklist (L->next);       l->next->next=l;//Countdown first point to the penultimate l->next=null;//the penultimate pointer empty     return p; } node*noreverselinklist (Node*l) {if (l==null| |      L->next==null) return L;      Node*pnow=l,*pre=null,*nex=null,*tail=null;          while (pnow!=null) {nex=pnow->next;          if (Null==nex) {tail=pnow;}          pnow->next=pre;          Pre=pnow;                Pnow=nex;  } return tail;       } int _tmain (int argc, _tchar* argv[]) {int a[] = {1,2,3,4,5,6};      int b[] = {1,2,3,4,5,6,7};        Node *l = createlinklist (a,6);        Display (L);      Display (Noreverselinklist (L));       Node *a = createlinklist (b,7);      Display (Reverselinklist (A));      System ("pause");  return 0;   }

Thank you for reading. Detailed explanation: (with figure understanding) http://blog.csdn.net/zhaoruixiang1111/article/details/49932603

Reverse the order of a linked list C + +

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.