Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to delete duplicate elements with python

Shulou Source: shulou.com Published: 2022-06-01 11:24:54 10月03日 Update

This article mainly explains "how to delete duplicate elements with python". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "how to delete duplicate elements with python" together!

For a given ordered list, delete duplicate elements, leaving only elements that appear once.

Idea: When an element is compared to the next element, if they are the same, the next pointer of the current element points to the next pointer of the next element.

Language : c

/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* deleteDuplicates(struct ListNode* head) { struct ListNode* cur = (int *)malloc(sizeof(struct ListNode)); cur = head;while(cur != NULL){while(cur->next != NULL && cur->val == cur->next->val){ cur->next = cur->next->next; } cur = cur->next; }return head;}

Language : cpp

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* deleteDuplicates(ListNode* head) { ListNode* cur = head;while(cur != NULL){while(cur->next != NULL && cur->val == cur->next->val){ cur->next = cur->next->next; } cur = cur->next; } return head; }};

Language : python

# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object):def deleteDuplicates(self, head):""" :type head: ListNode :rtype: ListNode """ now = headwhile head:while head.next and head.val == head.next.val: head.next = head.next.next head = head.nextreturn now thank you for reading, the above is "how to use python to delete duplicate elements" content, after the study of this article, I believe everyone on how to use python to delete duplicate elements this problem has a deeper understanding, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Elements learning content ideas pointers the same that is situation direction articles more knowledge knowledge points articles follow questions topic meaning practice push research Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Technology Xiaomi vpn MySQL