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 element except 0 should be 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 

A simple solution is to store all linked list element in an array. Then move all elements of array to beginning. Finally copy array elements back to 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to move all zeros
// to the end of the linked list.
#include <bits/stdc++.h>
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 begining 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";
}
  
/* Drier 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;
}

chevron_right


Output:

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

Linked list after rearrangement
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.




Article Tags :
Practice Tags :


1


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