# Hackerrank Delete duplicate-value nodes from a sorted linked list solution

In this HackerRank Delete duplicate-value nodes from a sorted linked list problem, have given pointers of the head of a sorted linked list, where the values in the nodes are ascending order. then we need to delete the nodes that have duplicate values and return the sorted list with each distinct value in the list. and if the given pointer is null then the list is empty.

## Problem solution in Java Programming.

### Problem solution in C++ programming.

```#include <bits/stdc++.h>

using namespace std;

public:
int data;

this->data = node_data;
this->next = nullptr;
}
};

public:

this->tail = nullptr;
}

void insert_node(int node_data) {

} else {
this->tail->next = node;
}

this->tail = node;
}
};

void print_singly_linked_list(SinglyLinkedListNode* node, string sep, ofstream& fout) {
while (node) {
fout << node->data;

node = node->next;

if (node) {
fout << sep;
}
}
}

while (node) {
SinglyLinkedListNode* temp = node;
node = node->next;

free(temp);
}
}

SinglyLinkedListNode *curr = llist;
while (curr != NULL) {
SinglyLinkedListNode *next = curr->next;
while (next != NULL && next->data == curr->data)
next = next->next;
curr->next = next;
curr = next;
if (next != NULL) next = next->next;
}
return llist;
}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

int t;
cin >> t;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

for (int t_itr = 0; t_itr < t; t_itr++) {

int llist_count;
cin >> llist_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

for (int i = 0; i < llist_count; i++) {
int llist_item;
cin >> llist_item;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

llist->insert_node(llist_item);
}

print_singly_linked_list(llist1, " ", fout);
fout << "\n";

}

fout.close();

return 0;
}```

### Problem solution in C programming.

```#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int data;
};

};

node->data = node_data;
node->next = NULL;

return node;
}

} else {
}

}

void print_singly_linked_list(SinglyLinkedListNode* node, char* sep, FILE* fptr) {
while (node) {
fprintf(fptr, "%d", node->data);

node = node->next;

if (node) {
fprintf(fptr, "%s", sep);
}
}
}

while (node) {
SinglyLinkedListNode* temp = node;
node = node->next;

free(temp);
}
}

SinglyLinkedListNode* nextBoi = currBoi->next;

while(nextBoi != NULL){
if(currBoi->data == nextBoi->data){
SinglyLinkedListNode* temp = nextBoi;
nextBoi = nextBoi->next;
currBoi->next = nextBoi;
temp->next = NULL;
free(temp);
} else {
currBoi = currBoi->next;
nextBoi = nextBoi->next;
}
}
}

int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");

char* t_endptr;
char* t_str = readline();
int t = strtol(t_str, &t_endptr, 10);

if (t_endptr == t_str || *t_endptr != '\0') { exit(EXIT_FAILURE); }

for (int t_itr = 0; t_itr < t; t_itr++) {
llist->tail = NULL;

char* llist_count_endptr;
char* llist_count_str = readline();
int llist_count = strtol(llist_count_str, &llist_count_endptr, 10);

if (llist_count_endptr == llist_count_str || *llist_count_endptr != '\0') { exit(EXIT_FAILURE); }

for (int i = 0; i < llist_count; i++) {
char* llist_item_endptr;
char* llist_item_str = readline();
int llist_item = strtol(llist_item_str, &llist_item_endptr, 10);

if (llist_item_endptr == llist_item_str || *llist_item_endptr != '\0') { exit(EXIT_FAILURE); }

}

char *sep = " ";

fprintf(fptr, "\n");

}

fclose(fptr);

return 0;
}

size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);

while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);

if (!line) { break; }

data_length += strlen(cursor);

if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }

size_t new_length = alloc_length << 1;
data = realloc(data, new_length);

if (!data) { break; }

alloc_length = new_length;
}

if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';
}

data = realloc(data, data_length);

return data;
}
```

### Problem solution in JavaScript programming.

'use strict'; const fs = require('fs'); process.stdin.resume(); process.stdin.setEncoding('utf-8'); let inputString = ''; let currentLine = 0; process.stdin.on('data', inputStdin => { inputString += inputStdin; }); process.stdin.on('end', _ => { inputString = inputString.replace(/\s*\$/, '') .split('\n') .map(str => str.replace(/\s*\$/, '')); main(); }); function readLine() { return inputString[currentLine++]; } const SinglyLinkedListNode = class { constructor(nodeData) { this.data = nodeData; this.next = null; } }; const SinglyLinkedList = class { constructor() { this.head = null; this.tail = null; } insertNode(nodeData) { const node = new SinglyLinkedListNode(nodeData); if (this.head == null) { this.head = node; } else { this.tail.next = node; } this.tail = node; } }; function printSinglyLinkedList(node, sep, ws) { while (node != null) { ws.write(String(node.data)); node = node.next; if (node != null) { ws.write(sep); } } } function removeDuplicates(head) { if (!head) return head; var node = head; while (node) { if (node.next && node.data === node.next.data) { node.next = node.next.next; } else { node = node.next; } } return head; } function main() { const ws = fs.createWriteStream(process.env.OUTPUT_PATH); const t = parseInt(readLine(), 10); for (let tItr = 0; tItr < t; tItr++) { const llistCount = parseInt(readLine(), 10); let llist = new SinglyLinkedList(); for (let i = 0; i < llistCount; i++) { const llistItem = parseInt(readLine(), 10); llist.insertNode(llistItem); } let llist1 = removeDuplicates(llist.head); printSinglyLinkedList(llist1, " ", ws) ws.write("\n"); } ws.end(); }