9 Series offer to prove safety: reverse list

Two ideas:

1. The coming of a linked list a reverse junction, it requires three pointers, respectively, before the junction point, the current node after the node.

2. traversing the header, each traversing a node, the node into the first node of a new list.

Let me talk first, I did this time around a bit, seriously node pointer picture straightened out ideas. Notes the idea explained very clearly the following code:

. 1 #include <the iostream>
 2  the using  namespace STD;
 . 3  struct ListNode {
 . 4  int Val;
 . 5  struct ListNode * Next;                             
 . 6 ListNode () {} // write is to add a default constructor, no parameters
 . 7  // Val (X), Next (NULL) 
. 8  
. 9  };
 10  class Solution {
 . 11  public :
 12 is      ListNode ReverseList * (* ListNode PHEAD) {
 13 is          IF (PHEAD == NULL)
 14              return NULL;
15          IF (pHead-> Next == NULL)
 16              return PHEAD;
 . 17          ListNode * P1 = PHEAD;
 18 is          ListNode * P2 = PHEAD;
 . 19          ListNode * P3 = PHEAD;
 20 is          P1 = pHead-> Next;
 21 is          P2-> Next = NULL;
 22 is          the while (P1 =! NULL)
 23 is          {
 24              P2 = P1-> next; // to point to the next node temporary pointer 
25              P1-> next = P3;
 26 is              // P3 = P2-> next ; 
27             p1 = p3; // always let p3 do before junction 
28              p1 = P2; // always let p1 do the current node 
29          }
 30          return p3; // has been done p1 current node, but later point p3 p1, it should return P3 
31 is  
32      }
 33 is  };
 34 is  int main ()
 35  {
 36      Solution sO;
 37 [      struct ListNode List [ . 4 ]; // suddenly realized, list as defined herein is no arguments, parameters must exist no constructor, but before I define a parameter constructor, so the system is not to define a default no-argument constructor
 38                              // to define a structure variable without parameters, it is necessary to add no-argument constructor 
39      List [ 0 ] .val =1;
40     list[0].next = &list[1];
41     list[1].val = 2;
42     list[1].next = &list[2];
43     list[2].val = 3;
44     list[2].next = &list[3];
45     list[3].val = 4;
46     list[3].next = NULL;
47     ListNode *re = so.ReverseList(list);
48     while(re!=NULL)
49     {
50         cout << re->val << endl;
51         re= re->next;
52     }
53     return 0;
54 }

I remember undergraduate c ++ class, the teacher emphasized, new and delete must be used simultaneously. So always think when I later delete pointer, including the definition pointer variable also want to delete. It is to open up a new space out, so it is necessary to release this space use delete. But when you define a pointer variable, it is just one variable, all the variables do not need to be released, the release of the memory space.

The second method, to re-establish a list:

 1 class Solution {
 2 public:
 3     ListNode* ReverseList(ListNode* pHead) {
 4         if (pHead == NULL)
 5             return NULL;
 6         ListNode*p1 = NULL;
 7         ListNode*p2 = NULL;
 8         ListNode*p3 = NULL;
 9         while (pHead != NULL)//头插法插入
10         {
11             p2 = p1;
12             p1 = pHead;
13             p3 = pHead->next;
14             p1->next = p2;
15             pHead = p3;
16         }
17         return p1; 
18 
19     }
20 };

This is the version I think the answer, in fact, almost with my ideas, my thoughts are it the original starting point of a node is taken out and then inserted into my head the new list. The idea is to answer directly to the first node of the original list point to the newly created list, so you can build one less variable.

code show as below:

. 1  class Solution {
 2  public :
 . 3      ListNode ReverseList * (* ListNode PHEAD) {
 . 4          IF (PHEAD == NULL)
 . 5              return NULL;
 . 6          ListNode * P = PHEAD;
 . 7          ListNode pNode * = NULL; // Create a new list junction point 
. 8          ListNode pNext * = NULL; // to hold the next original node list from loss 
. 9          pNode = P;             // first node to establish a first 
10          pNext = p-> next;
 . 11          Pnode- > Next = NULL;
 12 is         p = Pnext;
13         while (p!= NULL)
14         {
15             Pnext = p->next;
16             p->next = Pnode;
17             Pnode = p;
18             p = Pnext;
19         }
20         return Pnode; 
21 
22     }
23 };

In fact, no need to first node separate out, while there can also be placed directly on, so get the most streamlined version:

. 1  class Solution {
 2  public :
 . 3      ListNode ReverseList * (* ListNode PHEAD) {
 . 4          IF (PHEAD == NULL)
 . 5              return NULL;
 . 6          ListNode * P = PHEAD;
 . 7          ListNode pNode * = NULL; // Create a new list junction point 
. 8          ListNode pNext * = NULL; // to hold the next original node list from loss 
. 9          the while (P = NULL!) // first interpolation insert 
10          {
 . 11              pNext = p-> next;
 12 is              P -> next = Pnode;
13             Pnode = p;
14             p = Pnext;
15         }
16         return Pnode; 
17 
18     }
19 };

This problem is significant representatives, it is recommended to use the second, simpler, and clearer thinking. Only you need to create two pointers point to the next node in the list and the new list of the original.

Guess you like

Origin www.cnblogs.com/neverland0718/p/10987769.html