The one-way linked list packet

Problem:
  [Title] a given value of the head node type head, the one-way linked list node is an integer, to give a predetermined integer pivot.
    Implement a function of adjusting the list, the list will be adjusted to the left pivot portion is less than the value of the node,
    the intermediate portion of the pivot is equal to the value of the node, the right portion is greater than the pivot node.
    In addition to this requirement, there is no more requirement for the node order adjusted.
    For example: list 9-> 0-> 4-> 5-> 1, pivot = 3.
    List may be adjusted 1-> 0-> 4-> 9-> 5,
    may be a 0-> 1-> 9-> 5-> 4.
    In short, the left portion is less than meet the node 3,
    the intermediate portion is equal to the node 3 (in this case this part is empty),
    the right part is a node can be greater than 3. On a part of the internal node order is not required.
  Advanced:
    on the requirements of the original problem and then add the following two requirements. In the left, inside the right portion of the three requirements to do the order
    required in each section of node order from left to right is consistent with the order of the original node in the list.
    For example: list 9-> 0-> 4-> 5-> 1, pivot = 3. The list is adjusted 0-> 1-> 9-> 4-> 5.
    While meeting the requirements of the original problem, the left part of the node from left to right 0,1. In the former occurs first linked list is 0, 1 appears after;
    middle section is empty, not discussed in the present embodiment; node from left to right and right portions 9,4,5. In the original the list also appear first 9, then appeared 4, last seen 5.

    If the chain length is N, the time complexity requested to achieve O (N), to achieve the requested additional space complexity O (. 1)


Solution:
    using two pointers, to store the number of interpolating decimal pr, pr forward runs to store the number equal to the number , p is a pointer to traverse swimming

 

Code:

  

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 struct Node
 6 {
 7     int val;
 8     Node* next;
 9     Node(int a = 0) :val(a), next(NULL) {}
10 };
11 
12 void Partition(Node*& head, const int num)
13 {
14     Node *pr, *p;
15     p = pr = head;
16     while (p->next)
17     {
18         if (p->next->val < num)
19         {
20             Node* q;
21             q = pr->next;
22             pr->next = p->next;
23             p->next = p->next->next;
24             pr = pr->next;
25             pr->next = q;
26             //p2 = p1;
27         }
28         else if (p->next->val == num)
29         {
30             Node* q;
31             q = pr->next;
32             pr->next = p->next;
33             p->next = p->next->next;
34             pr->next->next = q;
35         }
36         else
37             p = p->next;
38     }        
39 
40 }
41 
42 
43 void Test()
44 {
45     int a[] = { 7,2,8,1,4,5,4,6 };
46     Node* head = new Node(-1);
47     Node* p = head;
48     for (auto n : a)
49     {
50         Node* q = new Node(n);
51         p->next = q;
52         p = q;
53     }
54     p->next = NULL;
55 
56     p = head->next;
57     cout << "原链表为: ";
58     while (p)
59     {
60         cout << p->val << "->";
61         p = p->next;
62     }
63 
64     Partition(head, 4);
65     p = head->next;
66     cout << endl << "*******************" << endl << "分部分后的链表为: ";
67     while (p)
68     {
69         cout << p->val << "->";
70         p = p->next;
71     }
72     cout << endl << "=============================" << endl;
73 }

 

Reproduced in: https: //www.cnblogs.com/zzw1024/p/10989395.html

Guess you like

Origin blog.csdn.net/weixin_34336526/article/details/93252298