## Algorithm

Problem Name: 30 days of code - Day 24: More Linked Lists

Objective
Check out the Tutorial tab for learning materials and an instructional video!

A Node class is provided for you in the editor. A Node object has an integer data field, data, and a Node instance pointer, next , pointing to another node (i.e.: the next node in a list). A removeDuplicates function is declared in your editor, which takes a pointer to the head node of a linked list as a parameter. Complete removeDuplicates so that it deletes any duplicate nodes from the list and returns the head of the updated list.

Note: The head pointer may be null, indicating that the list is empty. Be sure to reset your next pointer when performing deletions to avoid breaking the list.

Input Format

You do not need to read any input from stdin. The following input is handled by the locked stub code and passed to the removeDuplicates function:
The first line contains an integer, N, the number of nodes to be inserted.
The N subsequent lines each contain an integer describing the

value of a node being inserted at the list's tail.

Constraints

• The data elements of the linked list argument will always be in non-decreasing order.

Output Format

Your removeDuplicates function should return the head of the updated linked list. The locked stub code in your editor will print the returned list to stdout.

Sample Input

6
1
2
2
3
3
4


Sample Output

1 2 3 4 

## Code Examples

### #1 Code Example with C Programming

Code - C Programming


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

typedef struct Node
{
int data;
struct Node* next;
}Node;

while (node->next) {
if (node->data == node->next->data)
node->next = node->next->next;
else
node = node->next;
}

}

{
Node *p = (Node*)malloc(sizeof(Node));
p->data = data;
p->next=NULL;

}
{

}
else{
while(start->next!=NULL)
start=start->next;

start->next=p;

}
}
{
while(start)
{
printf("%d ",start->data);
start=start->next;
}
}
int main()
{
int T,data;
scanf("%d",&T);
while(T-->0){
scanf("%d",&data);
}

}

Copy The Code &

### #2 Code Example with C++ Programming

Code - C++ Programming


#include <cmath>
#include <iostream>

using namespace std;

class Node {
public:
int data;
Node *next;

Node(int d) {
data = d;
next = NULL;
}
};

class Solution {
public:
while (curr && curr->next) {
while (curr->next && curr->data == curr->next->data) {
curr->next = curr->next->next;
}
curr = curr->next;
}
}

Node *insert(Node *head, int data) {
Node *p = new Node(data);

} else if (head->next == NULL) {

} else {
while (start->next != NULL) {
start = start->next;
}
start->next = p;

}

}

while (start) {
cout << start->data << " ";
start = start->next;
}
}
};

int main() {
Solution mylist;
int T, data;
cin >> T;
while (T-- > 0) {
cin >> data;
}
}

Copy The Code &

### #3 Code Example with C# Programming

Code - C# Programming


using System;
using System.Collections.Generic;
class Node
{
public int data;
public Node next;
public Node(int d){
data=d;
next=null;
}

}
class Solution {

while (curr != null && curr.next != null)
{
while (curr.next != null && curr.data == curr.next.data)
{
curr.next = curr.next.next;
}
curr = curr.next;
}
}

public static  Node insert(Node head,int data)
{
Node p=new Node(data);

else
{
while(start.next!=null)
start=start.next;
start.next=p;

}
}
{
while(start!=null)
{
Console.Write(start.data+" ");
start=start.next;
}
}
static void Main(String[] args) {

while(T-->0){
}
}
}


Copy The Code &

### #4 Code Example with Java Programming

Code - Java Programming


import java.util.Scanner;

class Node {
int data;
Node next;

Node(int d) {
data = d;
next = null;
}
}

class Solution {
public static Node removeDuplicates(Node head) {
while (curr != null && curr.next != null) {
while (curr.next != null && curr.data == curr.next.data) {
curr.next = curr.next.next;
}
curr = curr.next;
}
}

public static Node insert(Node head, int data) {
Node p = new Node(data);
else {
while (start.next != null)
start = start.next;
start.next = p;

}
}

public static void display(Node head) {
while (start != null) {
System.out.print(start.data + " ");
start = start.next;
}
}

public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while (T-- > 0) {
int ele = sc.nextInt();
}
}
}

Copy The Code &

### #5 Code Example with Javascript Programming

Code - Javascript Programming


process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
input_stdin += data;
});

process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
return input_stdin_array[input_currentline++];
}
function Node(data){
this.data=data;
this.next=null;
}
function Solution(){

if (!current) return;
while (current.next) {
if (current.data === current.next.data) {
current.next = current.next.next
} else {
current = current.next
}
}
}

var p=new Node(data);
}
}
else{
while(start.next!=null){
start=start.next;
}
start.next=p;
}

};

while(start){
process.stdout.write(start.data+" ");
start=start.next;
}
};
}
function main(){
var mylist=new Solution();
for(i = 0; i < T; i++){
}
}

Copy The Code &

### #6 Code Example with Python Programming

Code - Python Programming


class Node:
def __init__(self,data):
self.data = data
self.next = None
class Solution:
p = Node(data)
else:
while(start.next!=None):
start=start.next
start.next=p
while current:
print(current.data,end=' ')
current = current.next

while curr is not None and curr.next is not None:
while curr.next is not None and curr.data is curr.next.data:
curr.next = curr.next.next
curr = curr.next

mylist= Solution()
T=int(input())
for i in range(T):
data=int(input())

Copy The Code &

### #7 Code Example with PHP Programming

Code - PHP Programming


data = $d;$this->next = NULL;
}
}
class Solution{

function removeDuplicates($head){ //Write your code here$currentNode = $head; while ($currentNode != null && $currentNode->next != null) {$node = $currentNode; while ($node->next != null) {
if ($node->next->data ===$currentNode->data) {
$next =$node->next->next;
$node->next =$next;
} else {
$node =$node->next;
}
}
$currentNode =$currentNode->next;
}

return $head; } function insert($head,$data){ //complete this method$p=new Node($data); if($head==null){
$head=$p;
}
else if($head->next==null){$head->next=$p; } else{$start=$head; while($start->next!=null){
$start=$start->next;
}
$start->next=$p;
}
return $head; } function display($head){
$start=$head;
while($start){ echo$start->data,' ';
$start=$start->next;
}
}
}
$T=intval(fgets(STDIN));$head=null;
$mylist=new Solution(); while($T-->0){
$data=intval(fgets(STDIN));$head=$mylist->insert($head,$data); }$head=$mylist->removeDuplicates($head);
$mylist->display($head);
?>

Copy The Code &