# Move all zeros to the front of the linked list

Given a linked list. the task is to move all 0’s to the front of the linked list. The order of all other elements except 0 should be the same after rearrangement.

Examples:

```Input : 0 1 0 1 2 0 5 0 4 0
Output :0 0 0 0 0 1 1 2 5 4

Input :1 1 2 3 0 0 0
Output :0 0 0 1 1 2 3
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A simple solution is to store all linked list element in an array. Then move all elements of the array to the beginning. Finally, copy array elements back to the linked list.

An efficient solution is to traverse the linked list from second node. For every node with 0 value, we disconnect it from its current position and move the node to front.

 `// CPP program to move all zeros ` `// to the end of the linked list. ` `#include ` `using` `namespace` `std; ` ` `  `/* Link list node */` `struct` `Node { ` `  ``int` `data; ` `  ``struct` `Node *next; ` `}; ` ` `  `/* Given a reference (pointer to pointer) to ` `the head of a list and an int, push a new ` `node on the front of the list. */` `void` `push(``struct` `Node **head_ref, ``int` `new_data) { ` `  ``struct` `Node *new_node = ``new` `Node; ` `  ``new_node->data = new_data; ` `  ``new_node->next = (*head_ref); ` `  ``(*head_ref) = new_node; ` `} ` ` `  `/* moving zeroes to the beginning in linked list */` `void` `moveZeroes(``struct` `Node **head) { ` `  ``if` `(*head == NULL) ` `    ``return``; ` ` `  `  ``// Traverse the list from second node. ` `  ``struct` `Node *temp = (*head)->next, *prev = *head; ` `  ``while` `(temp != NULL) { ` ` `  `    ``// If current node is 0, move to ` `    ``// beginning of linked list ` `    ``if` `(temp->data == 0) { ` ` `  `      ``// Disconnect node from its ` `      ``// current position ` `      ``Node *curr = temp; ` `      ``temp = temp->next; ` `      ``prev->next = temp; ` ` `  `      ``// Move to beginning ` `      ``curr->next = (*head); ` `      ``*head = curr; ` `    ``} ` ` `  `    ``// For non-zero values ` `    ``else` `{ ` `      ``prev = temp; ` `      ``temp = temp->next; ` `    ``} ` `  ``} ` `} ` ` `  `// function to displaying nodes ` `void` `display(``struct` `Node *head) { ` `  ``while` `(head != NULL) { ` `    ``cout << head->data << ``"->"``; ` `    ``head = head->next; ` `  ``} ` `  ``cout << ``"NULL"``; ` `} ` ` `  `/* Driver program to test above function*/` `int` `main() { ` ` `  `  ``/* Start with the empty list */` `  ``struct` `Node *head = NULL; ` ` `  `  ``/* Use push() to construct below list ` `  ``0->0->1->0->1->0->2->0->3->0 */` `  ``push(&head, 0); ` `  ``push(&head, 3); ` `  ``push(&head, 0); ` `  ``push(&head, 2); ` `  ``push(&head, 0); ` `  ``push(&head, 1); ` `  ``push(&head, 0); ` `  ``push(&head, 1); ` `  ``push(&head, 0); ` `  ``push(&head, 0); ` ` `  `  ``// displaying list before rearrangement ` `  ``cout << ``"Linked list before rearrangement\n"``; ` `  ``display(head); ` ` `  `  ``/* Check the move_zeroes function */` `  ``moveZeroes(&head); ` ` `  `  ``// displaying list after rearrangement ` `  ``cout << ``"\n Linked list after rearrangement \n"``; ` `  ``display(head); ` ` `  `  ``return` `0; ` `} `

Output:

```Linked list before rearrangement
0->0->1->0->1->0->2->0->3->0->NULL

0->0->0->0->0->0->1->1->2->3->NULL
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Akanksha_Rai, nidhi_biet

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.