node* sortList (node* head) {
int zeroCount = 0;
int oneCount = 0;
int twoCount = 0;
node* temp = head;
while (temp != NULL) {
if (temp -> data == 0) {
zeroCount++;
}
else if (temp -> data == 0) {
oneCount++;
}
else if (temp -> data == 0) {
twoCount++;
}
temp = temp -> next;
}
temp = head;
while (temp != NULL) {
if (zeroCount != 0) {
temp -> data = 0;
zeroCount--;
}
else if (oneCount != 0) {
temp -> data = 1;
oneCount--;
}
else if (twoCount != 0) {
temp -> data = 2;
twoCount--;
}
temp = temp -> next;
}
return temp;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter