File tree Expand file tree Collapse file tree 1 file changed +62
-0
lines changed Expand file tree Collapse file tree 1 file changed +62
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ using namespace std ;
3+ struct Node {
4+ int data;
5+ Node * next;
6+ Node (int val): data(val), next(nullptr ) {}
7+ };
8+ Node * sortList (Node * head){
9+ int countzero = 0 ;
10+ int countone = 0 ;
11+ int counttwo = 0 ;
12+ Node * temp = head;
13+ while (temp != NULL ){
14+ if (temp->data == 0 ){
15+ countzero++;
16+ }
17+ if (temp->data == 1 ){
18+ countone++;
19+ }
20+ if (temp->data == 2 ){
21+ counttwo++;
22+ }
23+ temp = temp->next ;
24+ }
25+ temp = head;
26+ while (countzero > 0 ){
27+ temp->data = 0 ;
28+ countzero--;
29+ temp = temp->next ;
30+ }
31+ while (countone > 0 ){
32+ temp->data = 1 ;
33+ countone--;
34+ temp = temp->next ;
35+ }
36+ while (counttwo > 0 ){
37+ temp->data = 2 ;
38+ counttwo--;
39+ }
40+ return head;
41+ }
42+ void printlist (Node * head){
43+ Node * temp = head;
44+ while (temp != NULL ){
45+ cout << temp->data << " " ;
46+ temp = temp->next ;
47+ }
48+ cout << endl;
49+ }
50+ int main (){
51+ Node * head = new Node (1 );
52+ head->next = new Node (0 );
53+ head->next ->next = new Node (2 );
54+ head->next ->next ->next = new Node (1 );
55+ head->next ->next ->next ->next = new Node (2 );
56+ cout << " Original Linked List: " ;
57+ printlist (head);
58+ head = sortList (head);
59+ cout << " Sorted Linked List: " ;
60+ printlist (head);
61+ return 0 ;
62+ }
You can’t perform that action at this time.
0 commit comments