Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

// Remove các phần tử trùng lặp trong linked list đã được sắp xếp

// Code C

#include <stdio.h>
#include <stdlib.h>

// Data Structure to store a linked list node


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

// Helper function to print given linked list


void printList(struct Node* head)
{
struct Node* ptr = head;
while (ptr)
{
printf("%d -> ", ptr->data);
ptr = ptr->next;
}

printf("null");
}

// Helper function to insert new Node in the beginning of the linked list
void push(struct Node** head, int data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}

// Remove duplicates from a sorted list


void RemoveDuplicates(struct Node* head)
{
// do nothing if the list is empty
if (head == NULL)
return;

struct Node* current = head;

// Compare current node with next node


while (current->next != NULL)
{
if (current->data == current->next->data)
{
struct Node* nextNext = current->next->next;
free(current->next);
current->next = nextNext;
}
else {
current = current->next; // only advance if no deletion
}
}
}
// main method
int main(void)
{
// input keys
int keys[] = {1, 2, 2, 2, 3, 4, 4, 5};
int n = sizeof(keys)/sizeof(keys[0]);

// points to the head node of the linked list


struct Node* head = NULL;

// construct linked list


for (int i = n-1; i >= 0; i--)
push(&head, keys[i]);

RemoveDuplicates(head);

// print linked list


printList(head);

return 0;
}

// Code Java

// A linked list node


class Node
{
int data;
Node next;

Node(int data, Node next) {


this.data = data;
this.next = next;
}
}

class Main
{
// Helper function to print given linked list
public static void printList(Node head)
{
Node ptr = head;
while (ptr != null)
{
System.out.print(ptr.data + " -> ");
ptr = ptr.next;
}

System.out.println("null");
}

// Remove duplicates from a sorted list


public static Node removeDuplicates(Node head)
{
// do nothing if the list is empty
if (head == null) {
return null;
}

Node current = head;

// Compare current node with next node


while (current.next != null)
{
if (current.data == current.next.data)
{
Node nextNext = current.next.next;
current.next = nextNext;
}
else {
current = current.next; // only advance if no deletion
}
}

return head;
}

public static void main(String[] args)


{
// input keys
int[] keys = {1, 2, 2, 2, 3, 4, 4, 5};

// points to the head node of the linked list


Node head = null;

// construct linked list


for (int i = keys.length - 1; i >= 0; i--) {
head = new Node(keys[i], head);
}

head = removeDuplicates(head);

// print linked list


printList(head);
}
}

// Code Python

# A linked list node


class Node:
def __init__(self, data=None, next=None):
self.data = data
self.next = next

# Helper function to print given linked list


def printList(head):

ptr = head
while ptr:
print(ptr.data, end=" -> ")
ptr = ptr.next

print("None")
# Remove duplicates from a sorted list
def removeDuplicates(head):

# do nothing if the list is empty


if head is None:
return None

current = head

# Compare current node with next node


while current.next:
if current.data == current.next.data:
nextNext = current.next.next
current.next = nextNext
else:
current = current.next # only advance if no deletion

return head

if __name__ == '__main__':

# input keys
keys = [1, 2, 2, 2, 3, 4, 4, 5]

# construct linked list


head = None
for i in reversed(range(len(keys))):
head = Node(keys[i], head)

head = removeDuplicates(head)

# print linked list


printList(head)

You might also like