-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathlist-sort-0-1-2.cpp
84 lines (74 loc) · 1.72 KB
/
list-sort-0-1-2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// http://www.geeksforgeeks.org/sort-a-linked-list-of-0s-1s-or-2s/
#include <iostream>
using namespace std;
class node {
public:
int data;
node *next;
node(int data) {
this->data = data;
this->next = NULL;
}
};
node* sortList(node *head); // time complexity O(n), space complexity O(1)
node* addnode(node *head, int data);
void printList(node *head);
node* sortList(node *head) {
if (head == NULL)
return head;
int count[3] = {0};
node *ptr;
// count occurence of each element
ptr = head;
while (ptr != NULL) {
count[ptr->data]++;
ptr = ptr->next;
}
// traverse list and copy each element into list until its count is zero
ptr = head;
int i = 0;
while(ptr != NULL) {
if (count[i] == 0)
i++;
ptr->data = i;
count[i]--;
ptr = ptr->next;
}
return head;
}
node* addnode(node *head, int data) {
if (head == NULL)
return new node(data);
node *ptr = head;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = new node(data);
return head;
}
void printList(node *head) {
if (head == NULL)
return;
while (head != NULL) {
cout<<head->data<<" ";
head = head->next;
}
}
int main() {
node *head = NULL;
head = addnode(head, 0);
head = addnode(head, 1);
head = addnode(head, 0);
head = addnode(head, 2);
head = addnode(head, 1);
head = addnode(head, 1);
head = addnode(head, 2);
head = addnode(head, 1);
head = addnode(head, 2);
cout<<"list: ";
printList(head);
cout<<endl<<"sort: ";
head = sortList(head);
printList(head);
cout<<endl<<endl;
return 0;
}