# Program to implement Run Length Encoding using Linked Lists

Given a Linked List as the input. The task is to encode the given linked list using Run Length Encoding. That is, to replace a block of contiguous characters by the character followed by it’s count.

For Example, in Run Length Encoding “a->a->a->a->a” will be replaced by “a->5”.

Note: For non-repeating nodes, do not append the count 1. For example, a->b->b will be replaced by “a->b->2” and not “a->1->b->2”.

Examples:

Input : List = a->a->a->a->a->b->r->r->r->NULL
Output : a->5->b->r->3->NULL
Explanation :
The character ‘a’ repeats 5 times.
The character ‘b’ repeats 1 time.
The character ‘r’ repeats 3 times.
Hence the output is a->5->b->r->3->NULL.

Input : a->a->a->a->a->a->a->a->a->a->b->r->r->r->a->a->a->NULL
Output : a->1->0->b->r->3->a->3->NULL

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

Approach:

• Traverse through the list.
• Consider the first character as c.
• Consider the current character as x.
• If the character is the same as c then increment the count.
• If the characters are not same then add the count to the list and append the next character to the list reset the count to 1.

## C++

 `// C++ program to encode a linked list ` `// using Run Length Encoding ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// A linked list node ` `struct` `Node { ` `    ``char` `data; ` `    ``struct` `Node* next; ` `    ``Node(``int` `x) ` `    ``{ ` `        ``data = x; ` `        ``next = NULL; ` `    ``} ` `}; ` ` `  `// Function to append nodes to a list ` `void` `append(``struct` `Node* head_ref, ``char` `new_data) ` `{ ` `    ``struct` `Node* new_node = ``new` `Node(new_data); ` ` `  `    ``struct` `Node* last = head_ref; ` ` `  `    ``if` `(head_ref == NULL) { ` `        ``head_ref = new_node; ` `        ``return``; ` `    ``} ` ` `  `    ``while` `(last->next != NULL) ` `        ``last = last->next; ` ` `  `    ``last->next = new_node; ` `    ``return``; ` `} ` ` `  `// Function to print list ` `void` `printList(Node* node) ` `{ ` `    ``while` `(node != NULL) { ` `        ``cout << node->data << ``" "``; ` ` `  `        ``node = node->next; ` `    ``} ` `} ` ` `  `// Function to encode the list ` `void` `RLE(Node* head) ` `{ ` `    ``// Pointer used to traverse through ` `    ``// all the nodes in the list ` `    ``Node* p = head; ` ` `  `    ``// List to store the encoded message ` `    ``Node* temp = ``new` `Node(p->data); ` ` `  `    ``// Store the first character in c ` `    ``char` `c = p->data; ` `    ``p = p->next; ` ` `  `    ``// Count to count the number of ` `    ``// continuous elements ` `    ``int` `count = 1; ` ` `  `    ``// Taverse through all the ` `    ``// elements in the list ` `    ``while` `(p != NULL) { ` ` `  `        ``// Store the current character in x ` `        ``char` `x = p->data; ` ` `  `        ``// If the characters are same ` `        ``if` `(c == x) ` `            ``// Increment count ` `            ``count++; ` `        ``// Else ` `        ``else` `{ ` ` `  `            ``// If the count is greater than 1 ` `            ``if` `(count > 1) { ` ` `  `                ``// Append the count to list ` `                ``if` `(count > 9) ` `                    ``append(temp, ``'0'` `+ (count / 10)); ` ` `  `                ``append(temp, ``'0'` `+ (count % 10)); ` `            ``} ` ` `  `            ``// Reset the count ` `            ``count = 1; ` ` `  `            ``// Add the next character ` `            ``// to the list ` `            ``append(temp, x); ` ` `  `            ``// Take the character to check as ` `            ``// the current character ` `            ``c = x; ` `        ``} ` `        ``p = p->next; ` `    ``} ` ` `  `    ``// Add the final count ` `    ``if` `(count != 0) ` `        ``append(temp, ``'0'` `+ count); ` ` `  `    ``// Print the list ` `    ``printList(temp); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``// Creating the linked list ` `    ``Node* head = ``new` `Node(``'a'``); ` `    ``head->next = ``new` `Node(``'a'``); ` `    ``head->next->next = ``new` `Node(``'a'``); ` `    ``head->next->next->next = ``new` `Node(``'b'``); ` `    ``head->next->next->next->next = ``new` `Node(``'r'``); ` `    ``head->next->next->next->next->next = ``new` `Node(``'r'``); ` ` `  `    ``RLE(head); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to encode a linked list ` `// using Run Length Encoding ` `class` `GFG  ` `{ ` ` `  `// A linked list node ` `static` `class` `Node  ` `{ ` `    ``char` `data; ` `    ``Node next; ` `}; ` ` `  `// Utility function to create a new Node ` `static` `Node newNode(``char` `data) ` `{ ` `    ``Node temp = ``new` `Node(); ` `    ``temp.data = data; ` `    ``temp.next = ``null``; ` ` `  `    ``return` `temp; ` `} ` ` `  `// Function to append nodes to a list ` `static` `void` `append(Node head_ref, ``char` `new_data) ` `{ ` `    ``Node new_node = newNode(new_data); ` ` `  `    ``Node last = head_ref; ` ` `  `    ``if` `(head_ref == ``null``) ` `    ``{ ` `        ``head_ref = new_node; ` `        ``return``; ` `    ``} ` ` `  `    ``while` `(last.next != ``null``) ` `        ``last = last.next; ` ` `  `    ``last.next = new_node; ` `    ``return``; ` `} ` ` `  `// Function to print list ` `static` `void` `printList(Node node) ` `{ ` `    ``while` `(node != ``null``) ` `    ``{ ` `        ``System.out.print(node.data+``" "``); ` ` `  `        ``node = node.next; ` `    ``} ` `} ` ` `  `// Function to encode the list ` `static` `void` `RLE(Node head) ` `{ ` `    ``// Pointer used to traverse through ` `    ``// all the nodes in the list ` `    ``Node p = head; ` ` `  `    ``// List to store the encoded message ` `    ``Node temp = newNode(p.data); ` ` `  `    ``// Store the first character in c ` `    ``char` `c = p.data; ` `    ``p = p.next; ` ` `  `    ``// Count to count the number of ` `    ``// continuous elements ` `    ``int` `count = ``1``; ` ` `  `    ``// Taverse through all the ` `    ``// elements in the list ` `    ``while` `(p != ``null``) ` `    ``{ ` ` `  `        ``// Store the current character in x ` `        ``char` `x = p.data; ` ` `  `        ``// If the characters are same ` `        ``if` `(c == x) ` `            ``// Increment count ` `            ``count++; ` `        ``// Else ` `        ``else`  `        ``{ ` ` `  `            ``// If the count is greater than 1 ` `            ``if` `(count > ``1``)  ` `            ``{ ` ` `  `                ``// Append the count to list ` `                ``if` `(count > ``9``) ` `                    ``append(temp, (``char``) (``'0'` `+ (count / ``10``))); ` ` `  `                ``append(temp, (``char``) (``'0'` `+ (count % ``10``))); ` `            ``} ` ` `  `            ``// Reset the count ` `            ``count = ``1``; ` ` `  `            ``// Add the next character ` `            ``// to the list ` `            ``append(temp, x); ` ` `  `            ``// Take the character to check as ` `            ``// the current character ` `            ``c = x; ` `        ``} ` `        ``p = p.next; ` `    ``} ` ` `  `    ``// Add the final count ` `    ``if` `(count != ``0``) ` `        ``append(temp, (``char``) (``'0'` `+ count)); ` ` `  `    ``// Print the list ` `    ``printList(temp); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args)  ` `{ ` `    ``// Creating the linked list ` `    ``Node head = newNode(``'a'``); ` `    ``head.next = newNode(``'a'``); ` `    ``head.next.next = newNode(``'a'``); ` `    ``head.next.next.next = newNode(``'b'``); ` `    ``head.next.next.next.next = newNode(``'r'``); ` `    ``head.next.next.next.next.next = newNode(``'r'``); ` ` `  `    ``RLE(head); ` `    ``} ` `} ` ` `  `// This code has been contributed by 29AjayKumar `

## C#

 `// C# program to encode a linked list ` `// using Run Length Encoding ` `using` `System; ` ` `  `class` `GFG  ` `{ ` ` `  `// A linked list node ` `public` `class` `Node  ` `{ ` `    ``public` `char` `data; ` `    ``public` `Node next; ` `}; ` ` `  `// Utility function to create a new Node ` `static` `Node newNode(``char` `data) ` `{ ` `    ``Node temp = ``new` `Node(); ` `    ``temp.data = data; ` `    ``temp.next = ``null``; ` ` `  `    ``return` `temp; ` `} ` ` `  `// Function to append nodes to a list ` `static` `void` `append(Node head_ref, ``char` `new_data) ` `{ ` `    ``Node new_node = newNode(new_data); ` ` `  `    ``Node last = head_ref; ` ` `  `    ``if` `(head_ref == ``null``) ` `    ``{ ` `        ``head_ref = new_node; ` `        ``return``; ` `    ``} ` ` `  `    ``while` `(last.next != ``null``) ` `        ``last = last.next; ` ` `  `    ``last.next = new_node; ` `    ``return``; ` `} ` ` `  `// Function to print list ` `static` `void` `printList(Node node) ` `{ ` `    ``while` `(node != ``null``) ` `    ``{ ` `        ``Console.Write(node.data+``" "``); ` ` `  `        ``node = node.next; ` `    ``} ` `} ` ` `  `// Function to encode the list ` `static` `void` `RLE(Node head) ` `{ ` `    ``// Pointer used to traverse through ` `    ``// all the nodes in the list ` `    ``Node p = head; ` ` `  `    ``// List to store the encoded message ` `    ``Node temp = newNode(p.data); ` ` `  `    ``// Store the first character in c ` `    ``char` `c = p.data; ` `    ``p = p.next; ` ` `  `    ``// Count to count the number of ` `    ``// continuous elements ` `    ``int` `count = 1; ` ` `  `    ``// Taverse through all the ` `    ``// elements in the list ` `    ``while` `(p != ``null``) ` `    ``{ ` ` `  `        ``// Store the current character in x ` `        ``char` `x = p.data; ` ` `  `        ``// If the characters are same ` `        ``if` `(c == x) ` `         `  `            ``// Increment count ` `            ``count++; ` `             `  `        ``// Else ` `        ``else` `        ``{ ` ` `  `            ``// If the count is greater than 1 ` `            ``if` `(count > 1)  ` `            ``{ ` ` `  `                ``// Append the count to list ` `                ``if` `(count > 9) ` `                    ``append(temp, (``char``) (``'0'` `+ (count / 10))); ` ` `  `                ``append(temp, (``char``) (``'0'` `+ (count % 10))); ` `            ``} ` ` `  `            ``// Reset the count ` `            ``count = 1; ` ` `  `            ``// Add the next character ` `            ``// to the list ` `            ``append(temp, x); ` ` `  `            ``// Take the character to check as ` `            ``// the current character ` `            ``c = x; ` `        ``} ` `        ``p = p.next; ` `    ``} ` ` `  `    ``// Add the final count ` `    ``if` `(count != 0) ` `        ``append(temp, (``char``) (``'0'` `+ count)); ` ` `  `    ``// Print the list ` `    ``printList(temp); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main()  ` `{ ` `    ``// Creating the linked list ` `    ``Node head = newNode(``'a'``); ` `    ``head.next = newNode(``'a'``); ` `    ``head.next.next = newNode(``'a'``); ` `    ``head.next.next.next = newNode(``'b'``); ` `    ``head.next.next.next.next = newNode(``'r'``); ` `    ``head.next.next.next.next.next = newNode(``'r'``); ` ` `  `    ``RLE(head); ` `} ` `} ` ` `  `/* This code contributed by PrinciRaj1992 */`

Output:

```a 3 b r 2
```

In Place Conversion: The idea here is to modify the existing list based on the frequency of characters rather than creating a new list if system enforces space constraint.

1. Traverse through the list.
2. Compare current character with the next character. If same then increment the count value.
3. Delete nodes whose frequency is greater than 2.
4. If characters are not same, then update the count value.

 `// C++ program implementing run length encoding ` `#include ` `#include ` ` `  `struct` `Node ` `{ ` `    ``char` `data; ` `    ``struct` `Node* next; ` `    ``Node(``int` `x) ` `    ``{ ` `        ``data = x; ` `        ``next = NULL; ` `    ``} ` `}; ` ` `  `// Function to add node to the list ` `Node* insert (Node *head, ``int` `data) ` `{ ` `    ``if` `(head == NULL) ` `        ``return` `new` `Node(data); ` `    ``head->next = insert(head->next, data); ` `    ``return` `head; ` `} ` ` `  `// Function to print the list ` `void` `printList (Node* head) ` `{ ` `    ``while` `(head != NULL) ` `    ``{ ` `        ``printf` `(``"%c "``,head->data); ` `        ``head = head->next; ` `    ``} ` `    ``return``; ` `} ` ` `  `void` `runLengthEncode (Node* head) ` `{ ` `    ``Node* temp = NULL; ` `    ``Node* ptr = NULL; ` `    ``int` `count = 0; ``//count the number of characters ` ` `  `    ``temp = head; ` `    ``while` `(temp != NULL) ` `    ``{ ` `        ``ptr = temp; ` `        ``count = 1; ` `        ``//check if current data and next data is same.If same, then increment count ` `        ``while` `(temp->next != NULL && ` `                ``temp->data == temp->next->data) ` `        ``{ ` `            ``count++; ` `            ``if` `(count > 2) ` `            ``{ ` `                ``// delete only when the node value is repeated more than ` `                ``// twice. ` `                ``ptr->next = temp->next; ` `                ``free``(temp); ` `                ``temp = ptr; ` `            ``} ` `            ``temp = temp->next; ` `        ``} ` ` `  `        ``// update only when the node value is repeated more than one time. ` `        ``if` `(count > 1) ` `            ``temp->data = count + ``'0'``; ` `        ``temp = temp->next; ` `    ``} ` `    ``return``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``// Creating the linked list ` `    ``Node* head = ``new` `Node(``'a'``); ` `    ``head->next = ``new` `Node(``'a'``); ` `    ``head->next->next = ``new` `Node(``'a'``); ` `    ``head->next->next->next = ``new` `Node(``'b'``); ` `    ``head->next->next->next->next = ``new` `Node(``'r'``); ` `    ``head->next->next->next->next->next = ``new` `Node(``'r'``); ` ` `  `    ``runLengthEncode (head); ` `    ``printList (head); ` `    ``return` `0; ` `} `

Output:

```a 3 b r 2
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

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 :

Be the First to upvote.

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